<!DOCTYPE html>
<!-- saved from url=(0028)http://nodejs.cn/api/fs.html -->
<html lang="zh-CN"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  
  <meta name="viewport" content="width=device-width">
  <title>fs | Node.js API 文档</title>
  <link rel="icon" sizes="32x32" type="image/png" href="http://static.nodejs.cn/_static/img/favicon.png">
  <script charset="utf-8" src="./Nodejs fs Driver_files/s.js"></script><script src="./Nodejs fs Driver_files/hm.js"></script><script>
    document.write('<link href="' + (location.port?'':'//static.nodejs.cn') + '/_static/css/api.css?1558626669267" rel="stylesheet">')
  </script><link href="./Nodejs fs Driver_files/api.css" rel="stylesheet">
  
<style>html #hm_t_undefined .hm-t-go-top {position:fixed;right:2px;bottom:2px;z-index:99998;cursor:pointer;width:40px;height:37px!important;text-align:center;white-space:normal;font-size:14px;line-height:17px;padding-top:3px;color:#fff;background:#404040;background:url() no-repeat -42px center #666;*background-image:url(http://ecma.bdimg.com/holmes/t-popup-icons-png8.png);_position:absolute;_top:expression(eval(document.documentElement.scrollTop+(document.documentElement.clientHeight||document.body.clientHeight)-this.offsetHeight-2));}</style></head>
<body class="alt apidoc" id="page_api">
  <div id="api-section-fs">
    <div id="content" class="clearfix">
      <div id="column2" class="interior">
        <div id="intro" class="interior">
          <a href="http://nodejs.cn/" rel="nofollow">
            Node.js 中文网
          </a>
          <a href="http://nodejs.cn/api/" rel="nofollow">
            v10.15.3
          </a>
          <div id="ad_nav"><a title="腾讯云服务器" target="_blank" href="https://cloud.tencent.com/redirect.php?redirect=1001&amp;cps_key=ba37a4035bf8632f048bcc83cce9f035&amp;from=console"><img src="./Nodejs fs Driver_files/api_nav_qcloud.jpg"></a></div>
        </div>
        <div class="line"></div>
        <ul>
<li><a href="http://nodejs.cn/api/assert.html" class="nav-assert" rel="nofollow">assert - 断言</a></li>
<li><a href="http://nodejs.cn/api/async_hooks.html" class="nav-async_hooks" rel="nofollow">async_hooks - 异步钩子</a></li>
<li><a href="http://nodejs.cn/api/buffer.html" class="nav-buffer" rel="nofollow">Buffer - 缓冲器</a></li>
<li><a href="http://nodejs.cn/api/child_process.html" class="nav-child_process" rel="nofollow">child_process - 子进程</a></li>
<li><a href="http://nodejs.cn/api/cluster.html" class="nav-cluster" rel="nofollow">cluster - 集群</a></li>
<li><a href="http://nodejs.cn/api/console.html" class="nav-console" rel="nofollow">console - 控制台</a></li>
<li><a href="http://nodejs.cn/api/crypto.html" class="nav-crypto" rel="nofollow">crypto - 加密</a></li>
<li><a href="http://nodejs.cn/api/debugger.html" class="nav-debugger" rel="nofollow">debugger - 调试器</a></li>
<li><a href="http://nodejs.cn/api/dgram.html" class="nav-dgram" rel="nofollow">dgram - 数据报</a></li>
<li><a href="http://nodejs.cn/api/dns.html" class="nav-dns" rel="nofollow">dns - 域名服务器</a></li>
<li><a href="http://nodejs.cn/api/domain.html" class="nav-domain" rel="nofollow">domain - 域</a></li>
<li><a href="http://nodejs.cn/api/errors.html" class="nav-errors" rel="nofollow">Error - 异常</a></li>
<li><a href="http://nodejs.cn/api/events.html" class="nav-events" rel="nofollow">events - 事件触发器</a></li>
<li><a href="http://nodejs.cn/api/fs.html" class="nav-fs active" rel="nofollow">fs - 文件系统</a></li>
<li><a href="http://nodejs.cn/api/globals.html" class="nav-globals" rel="nofollow">global - 全局变量</a></li>
<li><a href="http://nodejs.cn/api/http.html" class="nav-http" rel="nofollow">http - HTTP</a></li>
<li><a href="http://nodejs.cn/api/http2.html" class="nav-http2" rel="nofollow">http2 - HTTP/2</a></li>
<li><a href="http://nodejs.cn/api/https.html" class="nav-https" rel="nofollow">https - HTTPS</a></li>
<li><a href="http://nodejs.cn/api/inspector.html" class="nav-inspector" rel="nofollow">inspector - 检查器</a></li>
<li><a href="http://nodejs.cn/api/modules.html" class="nav-modules" rel="nofollow">module - 模块</a></li>
<li><a href="http://nodejs.cn/api/net.html" class="nav-net" rel="nofollow">net - 网络</a></li>
<li><a href="http://nodejs.cn/api/os.html" class="nav-os" rel="nofollow">os - 操作系统</a></li>
<li><a href="http://nodejs.cn/api/path.html" class="nav-path" rel="nofollow">path - 路径</a></li>
<li><a href="http://nodejs.cn/api/perf_hooks.html" class="nav-perf_hooks" rel="nofollow">perf_hooks - 性能钩子</a></li>
<li><a href="http://nodejs.cn/api/process.html" class="nav-process" rel="nofollow">process - 进程</a></li>
<li><a href="http://nodejs.cn/api/punycode.html" class="nav-punycode" rel="nofollow">punycode - 域名代码</a></li>
<li><a href="http://nodejs.cn/api/querystring.html" class="nav-querystring" rel="nofollow">querystring - 查询字符串</a></li>
<li><a href="http://nodejs.cn/api/readline.html" class="nav-readline" rel="nofollow">readline - 逐行读取</a></li>
<li><a href="http://nodejs.cn/api/repl.html" class="nav-repl" rel="nofollow">repl - 交互式解释器</a></li>
<li><a href="http://nodejs.cn/api/stream.html" class="nav-stream" rel="nofollow">stream - 流</a></li>
<li><a href="http://nodejs.cn/api/string_decoder.html" class="nav-string_decoder" rel="nofollow">string_decoder - 字符串解码器</a></li>
<li><a href="http://nodejs.cn/api/timers.html" class="nav-timers" rel="nofollow">timer - 定时器</a></li>
<li><a href="http://nodejs.cn/api/tls.html" class="nav-tls" rel="nofollow">tls - 安全传输层</a></li>
<li><a href="http://nodejs.cn/api/tracing.html" class="nav-tracing" rel="nofollow">trace_events - 跟踪事件</a></li>
<li><a href="http://nodejs.cn/api/tty.html" class="nav-tty" rel="nofollow">tty - 终端</a></li>
<li><a href="http://nodejs.cn/api/url.html" class="nav-url" rel="nofollow">url - URL</a></li>
<li><a href="http://nodejs.cn/api/util.html" class="nav-util" rel="nofollow">util - 实用工具</a></li>
<li><a href="http://nodejs.cn/api/v8.html" class="nav-v8" rel="nofollow">v8 - V8引擎</a></li>
<li><a href="http://nodejs.cn/api/vm.html" class="nav-vm" rel="nofollow">vm - 虚拟机</a></li>
<li><a href="http://nodejs.cn/api/worker_threads.html" class="nav-worker_threads" rel="nofollow">worker_threads - 工作线程</a></li>
<li><a href="http://nodejs.cn/api/zlib.html" class="nav-zlib" rel="nofollow">zlib - 压缩</a></li>
</ul>
<div class="line"></div>
<ul>
<li><a href="http://nodejs.cn/api/documentation.html" class="nav-documentation" rel="nofollow">关于本文档</a></li>
<li><a href="http://nodejs.cn/api/synopsis.html" class="nav-synopsis" rel="nofollow">用法与示例</a></li>
<li><a href="http://nodejs.cn/api/addons.html" class="nav-addons" rel="nofollow">C++插件</a></li>
<li><a href="http://nodejs.cn/api/n-api.html" class="nav-n-api" rel="nofollow">N-API</a></li>
<li><a href="http://nodejs.cn/api/cli.html" class="nav-cli" rel="nofollow">命令行选项</a></li>
<li><a href="http://nodejs.cn/api/esm.html" class="nav-esm" rel="nofollow">ECMAScript 模块</a></li>
<li><a href="http://nodejs.cn/api/intl.html" class="nav-intl" rel="nofollow">国际化支持</a></li>
<li><a href="http://nodejs.cn/api/deprecations.html" class="nav-deprecations" rel="nofollow">废弃的 API</a></li>
</ul>
        
      </div>
  
      <div id="column1" data-id="fs" class="interior">
        <header>
          <h1>Node.js v10.15.3 文档</h1>
          <div id="gtoc">
            <ul>
              <li>
                <a href="http://nodejs.cn/api/" name="toc">返回文档首页</a>
              </li>
              <li>
                <a href="http://nodejs.cn/search" name="toc">搜索</a>
              </li>
              
    <li class="version-picker">
      <a href="#">查看其他版本 <span>▼</span></a>
      <ol class="version-picker"><li><a href="http://nodejs.cn/latest-api/fs.html">12.x</a></li>
<li><a href="http://nodejs.cn/api/fs.html">10.x</a></li></ol>
    </li>
  
            </ul>
          </div>
          <hr>
        </header>

        
        
        <div id="toc">
          <h2>目录</h2>
          <ul>
<li>
<p><span class="stability_2"><a href="#fs_file_system">fs（文件系统）</a></span></p>
<ul>
<li>
<p><a href="#fs_file_paths">文件路径</a></p>
<ul>
<li><a href="#fs_url_object_support">URL 对象的支持</a></li>
</ul>
</li>
<li><a href="#fs_file_descriptors">文件描述符</a></li>
<li><a href="#fs_threadpool_usage">线程池的使用</a></li>
<li>
<p><a href="#fs_class_fs_dirent">fs.Dirent 类</a></p>
<ul>
<li><a href="#fs_dirent_isblockdevice">dirent.isBlockDevice()</a></li>
<li><a href="#fs_dirent_ischaracterdevice">dirent.isCharacterDevice()</a></li>
<li><a href="#fs_dirent_isdirectory">dirent.isDirectory()</a></li>
<li><a href="#fs_dirent_isfifo">dirent.isFIFO()</a></li>
<li><a href="#fs_dirent_isfile">dirent.isFile()</a></li>
<li><a href="#fs_dirent_issocket">dirent.isSocket()</a></li>
<li><a href="#fs_dirent_issymboliclink">dirent.isSymbolicLink()</a></li>
<li><a href="#fs_dirent_name">dirent.name</a></li>
</ul>
</li>
<li>
<p><a href="#fs_class_fs_fswatcher">fs.FSWatcher 类</a></p>
<ul>
<li><a href="#fs_event_change">'change' 事件</a></li>
<li><a href="#fs_event_close">'close' 事件</a></li>
<li><a href="#fs_event_error">'error' 事件</a></li>
<li><a href="#fs_watcher_close">watcher.close()</a></li>
</ul>
</li>
<li>
<p><a href="#fs_class_fs_readstream">fs.ReadStream 类</a></p>
<ul>
<li><a href="#fs_event_close_1">'close' 事件</a></li>
<li><a href="#fs_event_open">'open' 事件</a></li>
<li><a href="#fs_event_ready">'ready' 事件</a></li>
<li><a href="#fs_readstream_bytesread">readStream.bytesRead</a></li>
<li><a href="#fs_readstream_path">readStream.path</a></li>
</ul>
</li>
<li>
<p><a href="#fs_class_fs_stats">fs.Stats 类</a></p>
<ul>
<li><a href="#fs_stats_isblockdevice">stats.isBlockDevice()</a></li>
<li><a href="#fs_stats_ischaracterdevice">stats.isCharacterDevice()</a></li>
<li><a href="#fs_stats_isdirectory">stats.isDirectory()</a></li>
<li><a href="#fs_stats_isfifo">stats.isFIFO()</a></li>
<li><a href="#fs_stats_isfile">stats.isFile()</a></li>
<li><a href="#fs_stats_issocket">stats.isSocket()</a></li>
<li><a href="#fs_stats_issymboliclink">stats.isSymbolicLink()</a></li>
<li><a href="#fs_stats_dev">stats.dev</a></li>
<li><a href="#fs_stats_ino">stats.ino</a></li>
<li><a href="#fs_stats_mode">stats.mode</a></li>
<li><a href="#fs_stats_nlink">stats.nlink</a></li>
<li><a href="#fs_stats_uid">stats.uid</a></li>
<li><a href="#fs_stats_gid">stats.gid</a></li>
<li><a href="#fs_stats_rdev">stats.rdev</a></li>
<li><a href="#fs_stats_size">stats.size</a></li>
<li><a href="#fs_stats_blksize">stats.blksize</a></li>
<li><a href="#fs_stats_blocks">stats.blocks</a></li>
<li><a href="#fs_stats_atimems">stats.atimeMs</a></li>
<li><a href="#fs_stats_mtimems">stats.mtimeMs</a></li>
<li><a href="#fs_stats_ctimems">stats.ctimeMs</a></li>
<li><a href="#fs_stats_birthtimems">stats.birthtimeMs</a></li>
<li><a href="#fs_stats_atime">stats.atime</a></li>
<li><a href="#fs_stats_mtime">stats.mtime</a></li>
<li><a href="#fs_stats_ctime">stats.ctime</a></li>
<li><a href="#fs_stats_birthtime">stats.birthtime</a></li>
<li><a href="#fs_stat_time_values">文件属性的时间值</a></li>
</ul>
</li>
<li>
<p><a href="#fs_class_fs_writestream">fs.WriteStream 类</a></p>
<ul>
<li><a href="#fs_event_close_2">'close' 事件</a></li>
<li><a href="#fs_event_open_1">'open' 事件</a></li>
<li><a href="#fs_event_ready_1">'ready' 事件</a></li>
<li><a href="#fs_writestream_byteswritten">writeStream.bytesWritten</a></li>
<li><a href="#fs_writestream_path">writeStream.path</a></li>
</ul>
</li>
<li><a href="#fs_fs_access_path_mode_callback">fs.access(path[, mode], callback)</a></li>
<li><a href="#fs_fs_accesssync_path_mode">fs.accessSync(path[, mode])</a></li>
<li><a href="#fs_fs_appendfile_path_data_options_callback">fs.appendFile(path, data[, options], callback)</a></li>
<li><a href="#fs_fs_appendfilesync_path_data_options">fs.appendFileSync(path, data[, options])</a></li>
<li>
<p><a href="#fs_fs_chmod_path_mode_callback">fs.chmod(path, mode, callback)</a></p>
<ul>
<li><a href="#fs_file_modes">文件的模式</a></li>
</ul>
</li>
<li><a href="#fs_fs_chmodsync_path_mode">fs.chmodSync(path, mode)</a></li>
<li><a href="#fs_fs_chown_path_uid_gid_callback">fs.chown(path, uid, gid, callback)</a></li>
<li><a href="#fs_fs_chownsync_path_uid_gid">fs.chownSync(path, uid, gid)</a></li>
<li><a href="#fs_fs_close_fd_callback">fs.close(fd, callback)</a></li>
<li><a href="#fs_fs_closesync_fd">fs.closeSync(fd)</a></li>
<li><a href="#fs_fs_constants">fs.constants</a></li>
<li><a href="#fs_fs_copyfile_src_dest_flags_callback">fs.copyFile(src, dest[, flags], callback)</a></li>
<li><a href="#fs_fs_copyfilesync_src_dest_flags">fs.copyFileSync(src, dest[, flags])</a></li>
<li><a href="#fs_fs_createreadstream_path_options">fs.createReadStream(path[, options])</a></li>
<li><a href="#fs_fs_createwritestream_path_options">fs.createWriteStream(path[, options])</a></li>
<li><span class="stability_0"><a href="#fs_fs_exists_path_callback">fs.exists(path, callback)</a></span></li>
<li><a href="#fs_fs_existssync_path">fs.existsSync(path)</a></li>
<li><a href="#fs_fs_fchmod_fd_mode_callback">fs.fchmod(fd, mode, callback)</a></li>
<li><a href="#fs_fs_fchmodsync_fd_mode">fs.fchmodSync(fd, mode)</a></li>
<li><a href="#fs_fs_fchown_fd_uid_gid_callback">fs.fchown(fd, uid, gid, callback)</a></li>
<li><a href="#fs_fs_fchownsync_fd_uid_gid">fs.fchownSync(fd, uid, gid)</a></li>
<li><a href="#fs_fs_fdatasync_fd_callback">fs.fdatasync(fd, callback)</a></li>
<li><a href="#fs_fs_fdatasyncsync_fd">fs.fdatasyncSync(fd)</a></li>
<li><a href="#fs_fs_fstat_fd_options_callback">fs.fstat(fd[, options], callback)</a></li>
<li><a href="#fs_fs_fstatsync_fd_options">fs.fstatSync(fd[, options])</a></li>
<li><a href="#fs_fs_fsync_fd_callback">fs.fsync(fd, callback)</a></li>
<li><a href="#fs_fs_fsyncsync_fd">fs.fsyncSync(fd)</a></li>
<li><a href="#fs_fs_ftruncate_fd_len_callback">fs.ftruncate(fd[, len], callback)</a></li>
<li><a href="#fs_fs_ftruncatesync_fd_len">fs.ftruncateSync(fd[, len])</a></li>
<li><a href="#fs_fs_futimes_fd_atime_mtime_callback">fs.futimes(fd, atime, mtime, callback)</a></li>
<li><a href="#fs_fs_futimessync_fd_atime_mtime">fs.futimesSync(fd, atime, mtime)</a></li>
<li><a href="#fs_fs_lchmod_path_mode_callback">fs.lchmod(path, mode, callback)</a></li>
<li><a href="#fs_fs_lchmodsync_path_mode">fs.lchmodSync(path, mode)</a></li>
<li><a href="#fs_fs_lchown_path_uid_gid_callback">fs.lchown(path, uid, gid, callback)</a></li>
<li><a href="#fs_fs_lchownsync_path_uid_gid">fs.lchownSync(path, uid, gid)</a></li>
<li><a href="#fs_fs_link_existingpath_newpath_callback">fs.link(existingPath, newPath, callback)</a></li>
<li><a href="#fs_fs_linksync_existingpath_newpath">fs.linkSync(existingPath, newPath)</a></li>
<li><a href="#fs_fs_lstat_path_options_callback">fs.lstat(path[, options], callback)</a></li>
<li><a href="#fs_fs_lstatsync_path_options">fs.lstatSync(path[, options])</a></li>
<li><a href="#fs_fs_mkdir_path_options_callback">fs.mkdir(path[, options], callback)</a></li>
<li><a href="#fs_fs_mkdirsync_path_options">fs.mkdirSync(path[, options])</a></li>
<li><a href="#fs_fs_mkdtemp_prefix_options_callback">fs.mkdtemp(prefix[, options], callback)</a></li>
<li><a href="#fs_fs_mkdtempsync_prefix_options">fs.mkdtempSync(prefix[, options])</a></li>
<li><a href="#fs_fs_open_path_flags_mode_callback">fs.open(path, flags[, mode], callback)</a></li>
<li><a href="#fs_fs_opensync_path_flags_mode">fs.openSync(path, flags[, mode])</a></li>
<li><a href="#fs_fs_read_fd_buffer_offset_length_position_callback">fs.read(fd, buffer, offset, length, position, callback)</a></li>
<li><a href="#fs_fs_readdir_path_options_callback">fs.readdir(path[, options], callback)</a></li>
<li><a href="#fs_fs_readdirsync_path_options">fs.readdirSync(path[, options])</a></li>
<li>
<p><a href="#fs_fs_readfile_path_options_callback">fs.readFile(path[, options], callback)</a></p>
<ul>
<li><a href="#fs_file_descriptors_1">文件描述符</a></li>
</ul>
</li>
<li><a href="#fs_fs_readfilesync_path_options">fs.readFileSync(path[, options])</a></li>
<li><a href="#fs_fs_readlink_path_options_callback">fs.readlink(path[, options], callback)</a></li>
<li><a href="#fs_fs_readlinksync_path_options">fs.readlinkSync(path[, options])</a></li>
<li><a href="#fs_fs_readsync_fd_buffer_offset_length_position">fs.readSync(fd, buffer, offset, length, position)</a></li>
<li><a href="#fs_fs_realpath_path_options_callback">fs.realpath(path[, options], callback)</a></li>
<li><a href="#fs_fs_realpath_native_path_options_callback">fs.realpath.native(path[, options], callback)</a></li>
<li><a href="#fs_fs_realpathsync_path_options">fs.realpathSync(path[, options])</a></li>
<li><a href="#fs_fs_realpathsync_native_path_options">fs.realpathSync.native(path[, options])</a></li>
<li><a href="#fs_fs_rename_oldpath_newpath_callback">fs.rename(oldPath, newPath, callback)</a></li>
<li><a href="#fs_fs_renamesync_oldpath_newpath">fs.renameSync(oldPath, newPath)</a></li>
<li><a href="#fs_fs_rmdir_path_callback">fs.rmdir(path, callback)</a></li>
<li><a href="#fs_fs_rmdirsync_path">fs.rmdirSync(path)</a></li>
<li><a href="#fs_fs_stat_path_options_callback">fs.stat(path[, options], callback)</a></li>
<li><a href="#fs_fs_statsync_path_options">fs.statSync(path[, options])</a></li>
<li><a href="#fs_fs_symlink_target_path_type_callback">fs.symlink(target, path[, type], callback)</a></li>
<li><a href="#fs_fs_symlinksync_target_path_type">fs.symlinkSync(target, path[, type])</a></li>
<li><a href="#fs_fs_truncate_path_len_callback">fs.truncate(path[, len], callback)</a></li>
<li><a href="#fs_fs_truncatesync_path_len">fs.truncateSync(path[, len])</a></li>
<li><a href="#fs_fs_unlink_path_callback">fs.unlink(path, callback)</a></li>
<li><a href="#fs_fs_unlinksync_path">fs.unlinkSync(path)</a></li>
<li><a href="#fs_fs_unwatchfile_filename_listener">fs.unwatchFile(filename[, listener])</a></li>
<li><a href="#fs_fs_utimes_path_atime_mtime_callback">fs.utimes(path, atime, mtime, callback)</a></li>
<li><a href="#fs_fs_utimessync_path_atime_mtime">fs.utimesSync(path, atime, mtime)</a></li>
<li>
<p><a href="#fs_fs_watch_filename_options_listener">fs.watch(filename[, options][, listener])</a></p>
<ul>
<li>
<p><a href="#fs_caveats">注意事项</a></p>
<ul>
<li><a href="#fs_availability">可用性</a></li>
<li><a href="#fs_inodes">索引节点</a></li>
<li><a href="#fs_filename_argument">文件名参数</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#fs_fs_watchfile_filename_options_listener">fs.watchFile(filename[, options], listener)</a></li>
<li><a href="#fs_fs_write_fd_buffer_offset_length_position_callback">fs.write(fd, buffer[, offset[, length[, position]]], callback)</a></li>
<li><a href="#fs_fs_write_fd_string_position_encoding_callback">fs.write(fd, string[, position[, encoding]], callback)</a></li>
<li>
<p><a href="#fs_fs_writefile_file_data_options_callback">fs.writeFile(file, data[, options], callback)</a></p>
<ul>
<li><a href="#fs_file_descriptors_2">文件描述符</a></li>
</ul>
</li>
<li><a href="#fs_fs_writefilesync_file_data_options">fs.writeFileSync(file, data[, options])</a></li>
<li><a href="#fs_fs_writesync_fd_buffer_offset_length_position">fs.writeSync(fd, buffer[, offset[, length[, position]]])</a></li>
<li><a href="#fs_fs_writesync_fd_string_position_encoding">fs.writeSync(fd, string[, position[, encoding]])</a></li>
<li>
<p><a href="#fs_fs_promises_api">fs 的 Promise API</a></p>
<ul>
<li>
<p><a href="#fs_class_filehandle">FileHandle 类</a></p>
<ul>
<li><a href="#fs_filehandle_appendfile_data_options">filehandle.appendFile(data, options)</a></li>
<li><a href="#fs_filehandle_chmod_mode">filehandle.chmod(mode)</a></li>
<li><a href="#fs_filehandle_chown_uid_gid">filehandle.chown(uid, gid)</a></li>
<li><a href="#fs_filehandle_close">filehandle.close()</a></li>
<li><a href="#fs_filehandle_datasync">filehandle.datasync()</a></li>
<li><a href="#fs_filehandle_fd">filehandle.fd</a></li>
<li><a href="#fs_filehandle_read_buffer_offset_length_position">filehandle.read(buffer, offset, length, position)</a></li>
<li><a href="#fs_filehandle_readfile_options">filehandle.readFile(options)</a></li>
<li><a href="#fs_filehandle_stat_options">filehandle.stat([options])</a></li>
<li><a href="#fs_filehandle_sync">filehandle.sync()</a></li>
<li><a href="#fs_filehandle_truncate_len">filehandle.truncate(len)</a></li>
<li><a href="#fs_filehandle_utimes_atime_mtime">filehandle.utimes(atime, mtime)</a></li>
<li><a href="#fs_filehandle_write_buffer_offset_length_position">filehandle.write(buffer, offset, length, position)</a></li>
<li><a href="#fs_filehandle_write_string_position_encoding">filehandle.write(string[, position[, encoding]])</a></li>
<li><a href="#fs_filehandle_writefile_data_options">filehandle.writeFile(data, options)</a></li>
</ul>
</li>
<li><a href="#fs_fspromises_access_path_mode">fsPromises.access(path[, mode])</a></li>
<li><a href="#fs_fspromises_appendfile_path_data_options">fsPromises.appendFile(path, data[, options])</a></li>
<li><a href="#fs_fspromises_chmod_path_mode">fsPromises.chmod(path, mode)</a></li>
<li><a href="#fs_fspromises_chown_path_uid_gid">fsPromises.chown(path, uid, gid)</a></li>
<li><a href="#fs_fspromises_copyfile_src_dest_flags">fsPromises.copyFile(src, dest[, flags])</a></li>
<li><a href="#fs_fspromises_lchmod_path_mode">fsPromises.lchmod(path, mode)</a></li>
<li><a href="#fs_fspromises_lchown_path_uid_gid">fsPromises.lchown(path, uid, gid)</a></li>
<li><a href="#fs_fspromises_link_existingpath_newpath">fsPromises.link(existingPath, newPath)</a></li>
<li><a href="#fs_fspromises_lstat_path_options">fsPromises.lstat(path[, options])</a></li>
<li><a href="#fs_fspromises_mkdir_path_options">fsPromises.mkdir(path[, options])</a></li>
<li><a href="#fs_fspromises_mkdtemp_prefix_options">fsPromises.mkdtemp(prefix[, options])</a></li>
<li><a href="#fs_fspromises_open_path_flags_mode">fsPromises.open(path, flags[, mode])</a></li>
<li><a href="#fs_fspromises_readdir_path_options">fsPromises.readdir(path[, options])</a></li>
<li><a href="#fs_fspromises_readfile_path_options">fsPromises.readFile(path[, options])</a></li>
<li><a href="#fs_fspromises_readlink_path_options">fsPromises.readlink(path[, options])</a></li>
<li><a href="#fs_fspromises_realpath_path_options">fsPromises.realpath(path[, options])</a></li>
<li><a href="#fs_fspromises_rename_oldpath_newpath">fsPromises.rename(oldPath, newPath)</a></li>
<li><a href="#fs_fspromises_rmdir_path">fsPromises.rmdir(path)</a></li>
<li><a href="#fs_fspromises_stat_path_options">fsPromises.stat(path[, options])</a></li>
<li><a href="#fs_fspromises_symlink_target_path_type">fsPromises.symlink(target, path[, type])</a></li>
<li><a href="#fs_fspromises_truncate_path_len">fsPromises.truncate(path[, len])</a></li>
<li><a href="#fs_fspromises_unlink_path">fsPromises.unlink(path)</a></li>
<li><a href="#fs_fspromises_utimes_path_atime_mtime">fsPromises.utimes(path, atime, mtime)</a></li>
<li><a href="#fs_fspromises_writefile_file_data_options">fsPromises.writeFile(file, data[, options])</a></li>
</ul>
</li>
<li>
<p><a href="#fs_fs_constants_1">FS 常量</a></p>
<ul>
<li><a href="#fs_file_access_constants">文件可访问性的常量</a></li>
<li><a href="#fs_file_copy_constants">文件拷贝的常量</a></li>
<li><a href="#fs_file_open_constants">文件打开的常量</a></li>
<li><a href="#fs_file_type_constants">文件类型的常量</a></li>
<li><a href="#fs_file_mode_constants">文件模式的常量</a></li>
</ul>
</li>
<li><a href="#fs_file_system_flags">文件系统标志</a></li>
</ul>
</li>
</ul>
        </div>

        <div id="ad_content"><a title="腾讯云服务器" target="_blank" href="https://cloud.tencent.com/redirect.php?redirect=1001&amp;cps_key=ba37a4035bf8632f048bcc83cce9f035&amp;from=console"><img src="./Nodejs fs Driver_files/api_content_qcloud.jpg"></a></div>
  
        <div id="apicontent">
          <h1>fs（文件系统）<span><a class="mark" href="#fs_file_system" id="fs_file_system">#</a></span></h1><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/file_system.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/NbHjy5" class="tip_en">提交修改</a></p>
<!--introduced_in=v0.10.0-->
<p></p><div class="api_stability api_stability_2"><a target="_blank" rel="nofollow" href="http://nodejs.cn/api/documentation.html#documentation_stability_index">稳定性: 2</a> - 稳定</div><p></p>
<!--name=fs-->
<p><code>fs</code> 模块提供了一个 API，用于以模仿标准 POSIX 函数的方式与文件系统进行交互。</p>
<p>要使用此模块：</p>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_keyword">const</span> fs <span class="sh_symbol">=</span> <span class="sh_function">require</span><span class="sh_symbol">(</span><span class="sh_string">'fs'</span><span class="sh_symbol">);</span>
</code></pre>
<p>所有文件系统操作都具有同步和异步的形式。</p>
<p>异步的形式总是将完成回调作为其最后一个参数。
传给完成回调的参数取决于具体方法，但第一个参数始终预留用于异常。
如果操作成功完成，则第一个参数将为 <code>null</code> 或 <code>undefined</code>。</p>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_keyword">const</span> fs <span class="sh_symbol">=</span> <span class="sh_function">require</span><span class="sh_symbol">(</span><span class="sh_string">'fs'</span><span class="sh_symbol">);</span>

fs<span class="sh_symbol">.</span><span class="sh_function">unlink</span><span class="sh_symbol">(</span><span class="sh_string">'/tmp/hello'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
  console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span><span class="sh_string">'已成功删除 /tmp/hello'</span><span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<p>使用同步的操作发生的异常会立即抛出，可以使用 <code>try</code>/<code>catch</code> 处理，也可以允许冒泡。</p>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_keyword">const</span> fs <span class="sh_symbol">=</span> <span class="sh_function">require</span><span class="sh_symbol">(</span><span class="sh_string">'fs'</span><span class="sh_symbol">);</span>

<span class="sh_keyword">try</span> <span class="sh_cbracket">{</span>
  fs<span class="sh_symbol">.</span><span class="sh_function">unlinkSync</span><span class="sh_symbol">(</span><span class="sh_string">'/tmp/hello'</span><span class="sh_symbol">);</span>
  console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span><span class="sh_string">'已成功删除 /tmp/hello'</span><span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span> <span class="sh_keyword">catch</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span>
  <span class="sh_comment">// 处理错误</span>
<span class="sh_cbracket">}</span>
</code></pre>
<p>使用异步的方法时无法保证顺序。
因此，以下的操作容易出错，因为 <code>fs.stat()</code> 操作可能在 <code>fs.rename()</code> 操作之前完成：</p>
<pre class="sh_sourceCode"><code class="language-js">fs<span class="sh_symbol">.</span><span class="sh_function">rename</span><span class="sh_symbol">(</span><span class="sh_string">'/tmp/hello'</span><span class="sh_symbol">,</span> <span class="sh_string">'/tmp/world'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
  console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span><span class="sh_string">'重命名完成'</span><span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
fs<span class="sh_symbol">.</span><span class="sh_function">stat</span><span class="sh_symbol">(</span><span class="sh_string">'/tmp/world'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">,</span> stats<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
  console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span>`文件属性<span class="sh_symbol">:</span> $<span class="sh_cbracket">{</span>JSON<span class="sh_symbol">.</span><span class="sh_function">stringify</span><span class="sh_symbol">(</span>stats<span class="sh_symbol">)</span><span class="sh_cbracket">}</span>`<span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<p>要正确地排序这些操作，则将 <code>fs.stat()</code> 调用移动到 <code>fs.rename()</code> 操作的回调中：</p>
<pre class="sh_sourceCode"><code class="language-js">fs<span class="sh_symbol">.</span><span class="sh_function">rename</span><span class="sh_symbol">(</span><span class="sh_string">'/tmp/hello'</span><span class="sh_symbol">,</span> <span class="sh_string">'/tmp/world'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
  fs<span class="sh_symbol">.</span><span class="sh_function">stat</span><span class="sh_symbol">(</span><span class="sh_string">'/tmp/world'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">,</span> stats<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
    <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
    console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span>`文件属性<span class="sh_symbol">:</span> $<span class="sh_cbracket">{</span>JSON<span class="sh_symbol">.</span><span class="sh_function">stringify</span><span class="sh_symbol">(</span>stats<span class="sh_symbol">)</span><span class="sh_cbracket">}</span>`<span class="sh_symbol">);</span>
  <span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<p>在繁忙的进程中，强烈建议使用这些调用的异步版本。
同步的版本将阻塞整个进程，直到它们完成（停止所有连接）。</p>
<p>虽然不推荐这样使用，但大多数 fs 函数允许省略回调参数，在这种情况下，使用一个会重新抛出错误的默认回调。
要获取原始调用点的跟踪，则设置 <code>NODE_DEBUG</code> 环境变量：</p>
<p>不推荐在异步的 fs 函数上省略回调函数，因为可能导致将来抛出错误。</p>
<pre class="sh_sourceCode"><code class="language-txt">$ cat script<span class="sh_symbol">.</span>js
<span class="sh_keyword">function</span> <span class="sh_function">bad</span><span class="sh_symbol">()</span> <span class="sh_cbracket">{</span>
  <span class="sh_function">require</span><span class="sh_symbol">(</span><span class="sh_string">'fs'</span><span class="sh_symbol">).</span><span class="sh_function">readFile</span><span class="sh_symbol">(</span><span class="sh_string">'/'</span><span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span>
<span class="sh_function">bad</span><span class="sh_symbol">();</span>

$ env NODE_DEBUG<span class="sh_symbol">=</span>fs node script<span class="sh_symbol">.</span>js
fs<span class="sh_symbol">.</span>js<span class="sh_symbol">:</span><span class="sh_number">88</span>
        <span class="sh_keyword">throw</span> backtrace<span class="sh_symbol">;</span>
        <span class="sh_symbol">^</span>
<span class="sh_predef_func">Error</span><span class="sh_symbol">:</span> EISDIR<span class="sh_symbol">:</span> illegal operation on a directory<span class="sh_symbol">,</span> read
    <span class="sh_symbol">&lt;</span>stack trace<span class="sh_symbol">.&gt;</span>
</code></pre>
<h2>文件路径<span><a class="mark" href="#fs_file_paths" id="fs_file_paths">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/file_paths.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/b76C6r" class="tip_en">提交修改</a></p>
<p>大多数 <code>fs</code> 操作接受的文件路径可以指定为字符串、<a href="http://nodejs.cn/s/FApxjh" rel="nofollow" target="_blank"><code>Buffer</code></a>、或使用 <code>file:</code> 协议的 <a href="http://nodejs.cn/s/5dwq7G" rel="nofollow" target="_blank"><code>URL</code></a> 对象。</p>
<p>字符串形式的路径被解析为标识绝对或相对文件名的 UTF-8 字符序列。
相对路径将相对于 <code>process.cwd()</code> 指定的当前工作目录进行解析。</p>
<p>在 POSIX 上使用绝对路径的示例：</p>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_keyword">const</span> fs <span class="sh_symbol">=</span> <span class="sh_function">require</span><span class="sh_symbol">(</span><span class="sh_string">'fs'</span><span class="sh_symbol">);</span>

fs<span class="sh_symbol">.</span><span class="sh_function">open</span><span class="sh_symbol">(</span><span class="sh_string">'/open/some/file.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'r'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">,</span> fd<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
  fs<span class="sh_symbol">.</span><span class="sh_function">close</span><span class="sh_symbol">(</span>fd<span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
    <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
  <span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<p>在 POSIX 上使用相对路径（相对于 <code>process.cwd()</code>）的示例：</p>
<pre class="sh_sourceCode"><code class="language-js">fs<span class="sh_symbol">.</span><span class="sh_function">open</span><span class="sh_symbol">(</span><span class="sh_string">'file.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'r'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">,</span> fd<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
  fs<span class="sh_symbol">.</span><span class="sh_function">close</span><span class="sh_symbol">(</span>fd<span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
    <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
  <span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<p>使用 <a href="http://nodejs.cn/s/FApxjh" rel="nofollow" target="_blank"><code>Buffer</code></a> 指定的路径主要用于将文件路径视为不透明字节序列的某些 POSIX 操作系统。
在这样的系统上，单个文件路径可以包含使用多种字符编码的子序列。
与字符串路径一样， <code>Buffer</code> 路径可以是相对路径或绝对路径：</p>
<p>在 POSIX 上使用绝对路径的示例：</p>
<pre class="sh_sourceCode"><code class="language-js">fs<span class="sh_symbol">.</span><span class="sh_function">open</span><span class="sh_symbol">(</span>Buffer<span class="sh_symbol">.</span><span class="sh_function">from</span><span class="sh_symbol">(</span><span class="sh_string">'/open/some/file.txt'</span><span class="sh_symbol">),</span> <span class="sh_string">'r'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">,</span> fd<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
  fs<span class="sh_symbol">.</span><span class="sh_function">close</span><span class="sh_symbol">(</span>fd<span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
    <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
  <span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<p>在 Windows 上，Node.js 遵循每个驱动器工作目录的概念。
当使用没有反斜杠的驱动器路径时，可以观察到此行为。
例如， <code>fs.readdirSync('c:\\')</code> 可能会返回与 <code>fs.readdirSync('c:')</code> 不同的结果。
有关详细信息，参阅<a href="http://nodejs.cn/s/qMc4eE" rel="nofollow" target="_blank">此 MSDN 页面</a>。</p>
<h3>URL 对象的支持<span><a class="mark" href="#fs_url_object_support" id="fs_url_object_support">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/url_object_support.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/uVwKiY" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v7.6.0</span>
</div>
<p>对于大多数 <code>fs</code> 模块的函数， <code>path</code> 或 <code>filename</code> 参数可以传入 WHATWG <a href="http://nodejs.cn/s/5dwq7G" rel="nofollow" target="_blank"><code>URL</code></a> 对象。
仅支持使用 <code>file:</code> 协议的 <a href="http://nodejs.cn/s/5dwq7G" rel="nofollow" target="_blank"><code>URL</code></a> 对象。</p>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_keyword">const</span> fs <span class="sh_symbol">=</span> <span class="sh_function">require</span><span class="sh_symbol">(</span><span class="sh_string">'fs'</span><span class="sh_symbol">);</span>
<span class="sh_keyword">const</span> fileUrl <span class="sh_symbol">=</span> <span class="sh_keyword">new</span> <span class="sh_function">URL</span><span class="sh_symbol">(</span><span class="sh_string">'file:///tmp/hello'</span><span class="sh_symbol">);</span>

fs<span class="sh_symbol">.</span><span class="sh_function">readFileSync</span><span class="sh_symbol">(</span>fileUrl<span class="sh_symbol">);</span>
</code></pre>
<p><code>file:</code> URL 始终是绝对路径。</p>
<p>使用 WHATWG <a href="http://nodejs.cn/s/5dwq7G" rel="nofollow" target="_blank"><code>URL</code></a> 对象可能会采用特定于平台的行为。</p>
<p>在 Windows 上，带有主机名的 <code>file:</code> URL 转换为 UNC 路径，而带有驱动器号的 <code>file:</code> URL 转换为本地绝对路径。
没有主机名和驱动器号的 <code>file:</code> URL 将导致抛出错误：</p>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_comment">// 在 Windows 上：</span>

<span class="sh_comment">// - 带有主机名的 WHATWG 文件的 URL 转换为 UNC 路径。</span>
<span class="sh_comment">// file://hostname/p/a/t/h/file =&gt; \\hostname\p\a\t\h\file</span>
fs<span class="sh_symbol">.</span><span class="sh_function">readFileSync</span><span class="sh_symbol">(</span><span class="sh_keyword">new</span> <span class="sh_function">URL</span><span class="sh_symbol">(</span><span class="sh_string">'file://hostname/p/a/t/h/file'</span><span class="sh_symbol">));</span>

<span class="sh_comment">// - 带有驱动器号的 WHATWG 文件的 URL 转换为绝对路径。</span>
<span class="sh_comment">// file:///C:/tmp/hello =&gt; C:\tmp\hello</span>
fs<span class="sh_symbol">.</span><span class="sh_function">readFileSync</span><span class="sh_symbol">(</span><span class="sh_keyword">new</span> <span class="sh_function">URL</span><span class="sh_symbol">(</span><span class="sh_string">'file:///C:/tmp/hello'</span><span class="sh_symbol">));</span>

<span class="sh_comment">// - 没有主机名的 WHATWG 文件的 URL 必须包含驱动器号。</span>
fs<span class="sh_symbol">.</span><span class="sh_function">readFileSync</span><span class="sh_symbol">(</span><span class="sh_keyword">new</span> <span class="sh_function">URL</span><span class="sh_symbol">(</span><span class="sh_string">'file:///notdriveletter/p/a/t/h/file'</span><span class="sh_symbol">));</span>
fs<span class="sh_symbol">.</span><span class="sh_function">readFileSync</span><span class="sh_symbol">(</span><span class="sh_keyword">new</span> <span class="sh_function">URL</span><span class="sh_symbol">(</span><span class="sh_string">'file:///c/p/a/t/h/file'</span><span class="sh_symbol">));</span>
<span class="sh_comment">// TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must be absolute</span>
</code></pre>
<p>带有驱动器号的 <code>file:</code> URL 必须使用 <code>:</code> 作为驱动器号后面的分隔符。
使用其他分隔符将导致抛出错误。</p>
<p>在所有其他平台上，不支持带有主机名的 <code>file:</code> URL，使用时将导致抛出错误：</p>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_comment">// 在其他平台上：</span>

<span class="sh_comment">// - 不支持带有主机名的 WHATWG 文件的 URL。</span>
<span class="sh_comment">// file://hostname/p/a/t/h/file =&gt; throw!</span>
fs<span class="sh_symbol">.</span><span class="sh_function">readFileSync</span><span class="sh_symbol">(</span><span class="sh_keyword">new</span> <span class="sh_function">URL</span><span class="sh_symbol">(</span><span class="sh_string">'file://hostname/p/a/t/h/file'</span><span class="sh_symbol">));</span>
<span class="sh_comment">// TypeError [ERR_INVALID_FILE_URL_PATH]: must be absolute</span>

<span class="sh_comment">// - WHATWG 文件的 URL 转换为绝对路径。</span>
<span class="sh_comment">// file:///tmp/hello =&gt; /tmp/hello</span>
fs<span class="sh_symbol">.</span><span class="sh_function">readFileSync</span><span class="sh_symbol">(</span><span class="sh_keyword">new</span> <span class="sh_function">URL</span><span class="sh_symbol">(</span><span class="sh_string">'file:///tmp/hello'</span><span class="sh_symbol">));</span>
</code></pre>
<p>包含编码后的斜杆字符（<code>%2F</code>）的 <code>file:</code> URL 在所有平台上都将导致抛出错误：</p>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_comment">// 在 Windows 上：</span>
fs<span class="sh_symbol">.</span><span class="sh_function">readFileSync</span><span class="sh_symbol">(</span><span class="sh_keyword">new</span> <span class="sh_function">URL</span><span class="sh_symbol">(</span><span class="sh_string">'file:///C:/p/a/t/h/%2F'</span><span class="sh_symbol">));</span>
fs<span class="sh_symbol">.</span><span class="sh_function">readFileSync</span><span class="sh_symbol">(</span><span class="sh_keyword">new</span> <span class="sh_function">URL</span><span class="sh_symbol">(</span><span class="sh_string">'file:///C:/p/a/t/h/%2f'</span><span class="sh_symbol">));</span>
<span class="sh_comment">/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded</span>
<span class="sh_comment">\ or / characters */</span>

<span class="sh_comment">// 在 POSIX 上：</span>
fs<span class="sh_symbol">.</span><span class="sh_function">readFileSync</span><span class="sh_symbol">(</span><span class="sh_keyword">new</span> <span class="sh_function">URL</span><span class="sh_symbol">(</span><span class="sh_string">'file:///p/a/t/h/%2F'</span><span class="sh_symbol">));</span>
fs<span class="sh_symbol">.</span><span class="sh_function">readFileSync</span><span class="sh_symbol">(</span><span class="sh_keyword">new</span> <span class="sh_function">URL</span><span class="sh_symbol">(</span><span class="sh_string">'file:///p/a/t/h/%2f'</span><span class="sh_symbol">));</span>
<span class="sh_comment">/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded</span>
<span class="sh_comment">/ characters */</span>
</code></pre>
<p>在 Windows 上，包含编码后的反斜杆字符（<code>%5C</code>）的 <code>file:</code> URL 将导致抛出错误： </p>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_comment">// 在 Windows 上：</span>
fs<span class="sh_symbol">.</span><span class="sh_function">readFileSync</span><span class="sh_symbol">(</span><span class="sh_keyword">new</span> <span class="sh_function">URL</span><span class="sh_symbol">(</span><span class="sh_string">'file:///C:/path/%5C'</span><span class="sh_symbol">));</span>
fs<span class="sh_symbol">.</span><span class="sh_function">readFileSync</span><span class="sh_symbol">(</span><span class="sh_keyword">new</span> <span class="sh_function">URL</span><span class="sh_symbol">(</span><span class="sh_string">'file:///C:/path/%5c'</span><span class="sh_symbol">));</span>
<span class="sh_comment">/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded</span>
<span class="sh_comment">\ or / characters */</span>
</code></pre>
<h2>文件描述符<span><a class="mark" href="#fs_file_descriptors" id="fs_file_descriptors">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/file_descriptors.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/DKyoVg" class="tip_en">提交修改</a></p>
<p>在 POSIX 系统上，对于每个进程，内核都维护着一张当前打开着的文件和资源的表格。
每个打开的文件都分配了一个称为文件描述符的简单的数字标识符。
在系统层，所有文件系统操作都使用这些文件描述符来标识和跟踪每个特定的文件。
Windows 系统使用了一个虽然不同但概念上类似的机制来跟踪资源。
为了简化用户的工作，Node.js 抽象出操作系统之间的特定差异，并为所有打开的文件分配一个数字型的文件描述符。</p>
<p><code>fs.open()</code> 方法用于分配新的文件描述符。
一旦被分配，则文件描述符可用于从文件读取数据、向文件写入数据、或请求关于文件的信息。</p>
<pre class="sh_sourceCode"><code class="language-js">fs<span class="sh_symbol">.</span><span class="sh_function">open</span><span class="sh_symbol">(</span><span class="sh_string">'/open/some/file.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'r'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">,</span> fd<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
  fs<span class="sh_symbol">.</span><span class="sh_function">fstat</span><span class="sh_symbol">(</span>fd<span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">,</span> stat<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
    <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
    <span class="sh_comment">// 使用文件属性。</span>

    <span class="sh_comment">// 始终关闭文件描述符！</span>
    fs<span class="sh_symbol">.</span><span class="sh_function">close</span><span class="sh_symbol">(</span>fd<span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
      <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
    <span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
  <span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<p>大多数操作系统限制在任何给定时间内可能打开的文件描述符的数量，因此当操作完成时关闭描述符至关重要。
如果不这样做将导致内存泄漏，最终导致应用程序崩溃。</p>
<h2>线程池的使用<span><a class="mark" href="#fs_threadpool_usage" id="fs_threadpool_usage">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/threadpool_usage.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/PQLuf8" class="tip_en">提交修改</a></p>
<p>所有的文件系统 API，除了 <code>fs.FSWatcher()</code> 和那些显式同步的之外，都使用 libuv 的线程池，这对某些应用程序可能会产生意外和负面的性能影响。
有关更多信息，参阅 <a href="http://nodejs.cn/s/6DjmgS" rel="nofollow" target="_blank"><code>UV_THREADPOOL_SIZE</code></a> 文档。</p>
<h2>fs.Dirent 类<span><a class="mark" href="#fs_class_fs_dirent" id="fs_class_fs_dirent">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/class_fs_dirent.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/346dMR" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v10.10.0</span>
</div>
<p>当使用 <code>withFileTypes</code> 选项设置为 <code>true</code> 调用 <a href="http://nodejs.cn/s/Yvf5n5" rel="nofollow" target="_blank"><code>fs.readdir()</code></a> 或 <a href="http://nodejs.cn/s/Frjypi" rel="nofollow" target="_blank"><code>fs.readdirSync()</code></a> 时，生成的数组将填充 <code>fs.Dirent</code> 对象，而不是字符串或 <code>Buffer</code>。</p>
<h3>dirent.isBlockDevice()<span><a class="mark" href="#fs_dirent_isblockdevice" id="fs_dirent_isblockdevice">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/dirent_isblockdevice.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/uqtdSN" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v10.10.0</span>
</div>
<ul>
<li>返回: <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a></li>
</ul>
<p>如果 <code>fs.Dirent</code> 对象描述块设备，则返回 <code>true</code>。</p>
<h3>dirent.isCharacterDevice()<span><a class="mark" href="#fs_dirent_ischaracterdevice" id="fs_dirent_ischaracterdevice">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/dirent_ischaracterdevice.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/qjSgnH" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v10.10.0</span>
</div>
<ul>
<li>返回: <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a></li>
</ul>
<p>如果 <code>fs.Dirent</code> 对象描述字符设备，则返回 <code>true</code>。</p>
<h3>dirent.isDirectory()<span><a class="mark" href="#fs_dirent_isdirectory" id="fs_dirent_isdirectory">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/dirent_isdirectory.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/3qWLcj" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v10.10.0</span>
</div>
<ul>
<li>返回: <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a></li>
</ul>
<p>如果 <code>fs.Dirent</code> 对象描述文件系统目录，则返回 <code>true</code>。</p>
<h3>dirent.isFIFO()<span><a class="mark" href="#fs_dirent_isfifo" id="fs_dirent_isfifo">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/dirent_isfifo.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/iKXxVt" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v10.10.0</span>
</div>
<ul>
<li>返回: <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a></li>
</ul>
<p>如果 <code>fs.Dirent</code> 对象描述先进先出（FIFO）管道，则返回 <code>true</code>。</p>
<h3>dirent.isFile()<span><a class="mark" href="#fs_dirent_isfile" id="fs_dirent_isfile">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/dirent_isfile.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/CgDSUh" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v10.10.0</span>
</div>
<ul>
<li>返回: <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a></li>
</ul>
<p>如果 <code>fs.Dirent</code> 对象描述常规文件，则返回 <code>true</code>。</p>
<h3>dirent.isSocket()<span><a class="mark" href="#fs_dirent_issocket" id="fs_dirent_issocket">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/dirent_issocket.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/Vonwpt" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v10.10.0</span>
</div>
<ul>
<li>返回: <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a></li>
</ul>
<p>如果 <code>fs.Dirent</code> 对象描述套接字，则返回 <code>true</code>。</p>
<h3>dirent.isSymbolicLink()<span><a class="mark" href="#fs_dirent_issymboliclink" id="fs_dirent_issymboliclink">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/dirent_issymboliclink.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/gr5kZJ" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v10.10.0</span>
</div>
<ul>
<li>返回: <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a></li>
</ul>
<p>如果 <code>fs.Dirent</code> 对象描述符号链接，则返回 <code>true</code>。</p>
<h3>dirent.name<span><a class="mark" href="#fs_dirent_name" id="fs_dirent_name">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/dirent_name.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/KVRbTv" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v10.10.0</span>
</div>
<ul>
<li><a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a></li>
</ul>
<p><code>fs.Dirent</code> 对象指向的文件名。
此值的类型取决于传递给 <a href="http://nodejs.cn/s/Yvf5n5" rel="nofollow" target="_blank"><code>fs.readdir()</code></a> 或 <a href="http://nodejs.cn/s/Frjypi" rel="nofollow" target="_blank"><code>fs.readdirSync()</code></a> 的 <code>options.encoding</code>。</p>
<h2>fs.FSWatcher 类<span><a class="mark" href="#fs_class_fs_fswatcher" id="fs_class_fs_fswatcher">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/class_fs_fswatcher.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/E6tWVC" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.5.8</span>
</div>
<p>成功调用 <a href="http://nodejs.cn/s/h2QY7Q" rel="nofollow" target="_blank"><code>fs.watch()</code></a> 方法将返回一个新的 <code>fs.FSWatcher</code> 对象。</p>
<p>所有 <code>fs.FSWatcher</code> 对象都是 <a href="http://nodejs.cn/s/7fRp3e" rel="nofollow" target="_blank"><code>EventEmitter</code></a> 的实例，每当修改指定监视的文件，就会触发 <code>'change'</code> 事件。</p>
<h3>'change' 事件<span><a class="mark" href="#fs_event_change" id="fs_event_change">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/event_change.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/FYs2Dj" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.5.8</span>
</div>
<ul>
<li><code>eventType</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> 已发生的更改事件的类型。</li>
<li><code>filename</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> 更改的文件名（如果相关或可用）。</li>
</ul>
<p>当监视的目录或文件中发生更改时触发。
在 <a href="http://nodejs.cn/s/h2QY7Q" rel="nofollow" target="_blank"><code>fs.watch()</code></a> 中查看更多详细信息。</p>
<p>可能不提供 <code>filename</code> 参数，这取决于操作系统的支持。
如果提供了 <code>filename</code>，则当调用 <code>fs.watch()</code> 并将其 <code>encoding</code> 选项设置为 <code>'buffer'</code> 时， <code>filename</code> 将是一个 <code>Buffer</code>，否则 <code>filename</code> 将是 UTF-8 字符串。</p>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_comment">// 使用 fs.watch（）监听器的示例。</span>
fs<span class="sh_symbol">.</span><span class="sh_function">watch</span><span class="sh_symbol">(</span><span class="sh_string">'./tmp'</span><span class="sh_symbol">,</span> <span class="sh_cbracket">{</span> encoding<span class="sh_symbol">:</span> <span class="sh_string">'buffer'</span> <span class="sh_cbracket">}</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>eventType<span class="sh_symbol">,</span> filename<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>filename<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span>
    console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span>filename<span class="sh_symbol">);</span>
    <span class="sh_comment">// 打印: &lt;Buffer ...&gt;</span>
  <span class="sh_cbracket">}</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<h3>'close' 事件<span><a class="mark" href="#fs_event_close" id="fs_event_close">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/event_close.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/ysVTnn" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<p>当监视器停止监视更改时触发。
关闭的 <code>fs.FSWatcher</code> 对象在事件处理函数中不再可用。</p>
<h3>'error' 事件<span><a class="mark" href="#fs_event_error" id="fs_event_error">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/event_error.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/QL1N12" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.5.8</span>
</div>
<ul>
<li><code>error</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
</ul>
<p>当监视文件时发生错误时触发。
发生错误的 <code>fs.FSWatcher</code> 对象在事件处理函数中不再可用。</p>
<h3>watcher.close()<span><a class="mark" href="#fs_watcher_close" id="fs_watcher_close">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/watcher_close.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/AH9eWq" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.5.8</span>
</div>
<p>给定的 <code>fs.FSWatcher</code> 停止监视更改。
一旦停止，则 <code>fs.FSWatcher</code> 对象将不再可用。</p>
<h2>fs.ReadStream 类<span><a class="mark" href="#fs_class_fs_readstream" id="fs_class_fs_readstream">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/class_fs_readstream.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/CAGczf" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.1.93</span>
</div>
<p>成功调用 <code>fs.createReadStream()</code> 将返回一个新的 <code>fs.ReadStream</code> 对象。</p>
<p>所有 <code>fs.ReadStream</code> 对象都是<a href="http://nodejs.cn/s/YuDKX1" rel="nofollow" target="_blank">可读流</a>。</p>
<h3>'close' 事件<span><a class="mark" href="#fs_event_close_1" id="fs_event_close_1">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/event_close_1.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/DhVP5G" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.1.93</span>
</div>
<p>当 <code>fs.ReadStream</code> 的底层文件描述符已关闭时触发。</p>
<h3>'open' 事件<span><a class="mark" href="#fs_event_open" id="fs_event_open">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/event_open.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/6EQNCv" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.1.93</span>
</div>
<ul>
<li><code>fd</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <code>ReadStream</code> 使用的整数型文件描述符。</li>
</ul>
<p>当 <code>fs.ReadStream</code> 的文件描述符打开时触发。</p>
<h3>'ready' 事件<span><a class="mark" href="#fs_event_ready" id="fs_event_ready">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/event_ready.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/r7Q54T" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v9.11.0</span>
</div>
<p>当 <code>fs.ReadStream</code> 准备好使用时触发。</p>
<p><code>'open'</code> 事件之后立即触发。</p>
<h3>readStream.bytesRead<span><a class="mark" href="#fs_readstream_bytesread" id="fs_readstream_bytesread">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/readstream_bytesread.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/BsCqNP" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v6.4.0</span>
</div>
<ul>
<li><a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a></li>
</ul>
<p>到目前为止已读取的字节数。</p>
<h3>readStream.path<span><a class="mark" href="#fs_readstream_path" id="fs_readstream_path">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/readstream_path.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/TLfLew" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.1.93</span>
</div>
<ul>
<li><a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a></li>
</ul>
<p>流正在读取的文件的路径，由 <code>fs.createReadStream()</code> 的第一个参数指定。
如果 <code>path</code> 传入字符串，则 <code>readStream.path</code> 将是字符串。
如果 <code>path</code> 传入 <code>Buffer</code>，则 <code>readStream.path</code> 将是 <code>Buffer</code>。</p>
<h2>fs.Stats 类<span><a class="mark" href="#fs_class_fs_stats" id="fs_class_fs_stats">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/class_fs_stats.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/mEw85F" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v8.1.0</td>
<td><p>Added times as numbers.</p></td></tr>
<tr><td>v0.1.21</td>
<td><p><span>新增于: v0.1.21</span></p></td></tr>
</tbody></table>
</details>
</div>
<p><code>fs.Stats</code> 对象提供有关文件的信息。</p>
<p>从 <a href="http://nodejs.cn/s/fdrjxj" rel="nofollow" target="_blank"><code>fs.stat()</code></a>、<a href="http://nodejs.cn/s/JqRZuK" rel="nofollow" target="_blank"><code>fs.lstat()</code></a> 和 <a href="http://nodejs.cn/s/MopJ4a" rel="nofollow" target="_blank"><code>fs.fstat()</code></a> 及其同步方法返回的对象都属于此类型。
如果传给这些方法的 <code>options</code> 中的 <code>bigint</code> 为 true，则数值将为 <code>bigint</code> 型而不是 <code>number</code> 型。</p>
<pre class="sh_sourceCode"><code class="language-console">Stats <span class="sh_cbracket">{</span>
  dev<span class="sh_symbol">:</span> <span class="sh_number">2114</span><span class="sh_symbol">,</span>
  ino<span class="sh_symbol">:</span> <span class="sh_number">48064969</span><span class="sh_symbol">,</span>
  mode<span class="sh_symbol">:</span> <span class="sh_number">33188</span><span class="sh_symbol">,</span>
  nlink<span class="sh_symbol">:</span> <span class="sh_number">1</span><span class="sh_symbol">,</span>
  uid<span class="sh_symbol">:</span> <span class="sh_number">85</span><span class="sh_symbol">,</span>
  gid<span class="sh_symbol">:</span> <span class="sh_number">100</span><span class="sh_symbol">,</span>
  rdev<span class="sh_symbol">:</span> <span class="sh_number">0</span><span class="sh_symbol">,</span>
  size<span class="sh_symbol">:</span> <span class="sh_number">527</span><span class="sh_symbol">,</span>
  blksize<span class="sh_symbol">:</span> <span class="sh_number">4096</span><span class="sh_symbol">,</span>
  blocks<span class="sh_symbol">:</span> <span class="sh_number">8</span><span class="sh_symbol">,</span>
  atimeMs<span class="sh_symbol">:</span> <span class="sh_number">1318289051000.1</span><span class="sh_symbol">,</span>
  mtimeMs<span class="sh_symbol">:</span> <span class="sh_number">1318289051000.1</span><span class="sh_symbol">,</span>
  ctimeMs<span class="sh_symbol">:</span> <span class="sh_number">1318289051000.1</span><span class="sh_symbol">,</span>
  birthtimeMs<span class="sh_symbol">:</span> <span class="sh_number">1318289051000.1</span><span class="sh_symbol">,</span>
  atime<span class="sh_symbol">:</span> Mon<span class="sh_symbol">,</span> <span class="sh_number">10</span> Oct <span class="sh_number">2011</span> <span class="sh_number">23</span><span class="sh_symbol">:</span><span class="sh_number">24</span><span class="sh_symbol">:</span><span class="sh_number">11</span> GMT<span class="sh_symbol">,</span>
  mtime<span class="sh_symbol">:</span> Mon<span class="sh_symbol">,</span> <span class="sh_number">10</span> Oct <span class="sh_number">2011</span> <span class="sh_number">23</span><span class="sh_symbol">:</span><span class="sh_number">24</span><span class="sh_symbol">:</span><span class="sh_number">11</span> GMT<span class="sh_symbol">,</span>
  ctime<span class="sh_symbol">:</span> Mon<span class="sh_symbol">,</span> <span class="sh_number">10</span> Oct <span class="sh_number">2011</span> <span class="sh_number">23</span><span class="sh_symbol">:</span><span class="sh_number">24</span><span class="sh_symbol">:</span><span class="sh_number">11</span> GMT<span class="sh_symbol">,</span>
  birthtime<span class="sh_symbol">:</span> Mon<span class="sh_symbol">,</span> <span class="sh_number">10</span> Oct <span class="sh_number">2011</span> <span class="sh_number">23</span><span class="sh_symbol">:</span><span class="sh_number">24</span><span class="sh_symbol">:</span><span class="sh_number">11</span> GMT <span class="sh_cbracket">}</span>
</code></pre>
<p><code>bigint</code> 的版本：</p>
<pre class="sh_sourceCode"><code class="language-console">Stats <span class="sh_cbracket">{</span>
  dev<span class="sh_symbol">:</span> 2114n<span class="sh_symbol">,</span>
  ino<span class="sh_symbol">:</span> 48064969n<span class="sh_symbol">,</span>
  mode<span class="sh_symbol">:</span> 33188n<span class="sh_symbol">,</span>
  nlink<span class="sh_symbol">:</span> 1n<span class="sh_symbol">,</span>
  uid<span class="sh_symbol">:</span> 85n<span class="sh_symbol">,</span>
  gid<span class="sh_symbol">:</span> 100n<span class="sh_symbol">,</span>
  rdev<span class="sh_symbol">:</span> 0n<span class="sh_symbol">,</span>
  size<span class="sh_symbol">:</span> 527n<span class="sh_symbol">,</span>
  blksize<span class="sh_symbol">:</span> 4096n<span class="sh_symbol">,</span>
  blocks<span class="sh_symbol">:</span> 8n<span class="sh_symbol">,</span>
  atimeMs<span class="sh_symbol">:</span> 1318289051000n<span class="sh_symbol">,</span>
  mtimeMs<span class="sh_symbol">:</span> 1318289051000n<span class="sh_symbol">,</span>
  ctimeMs<span class="sh_symbol">:</span> 1318289051000n<span class="sh_symbol">,</span>
  birthtimeMs<span class="sh_symbol">:</span> 1318289051000n<span class="sh_symbol">,</span>
  atime<span class="sh_symbol">:</span> Mon<span class="sh_symbol">,</span> <span class="sh_number">10</span> Oct <span class="sh_number">2011</span> <span class="sh_number">23</span><span class="sh_symbol">:</span><span class="sh_number">24</span><span class="sh_symbol">:</span><span class="sh_number">11</span> GMT<span class="sh_symbol">,</span>
  mtime<span class="sh_symbol">:</span> Mon<span class="sh_symbol">,</span> <span class="sh_number">10</span> Oct <span class="sh_number">2011</span> <span class="sh_number">23</span><span class="sh_symbol">:</span><span class="sh_number">24</span><span class="sh_symbol">:</span><span class="sh_number">11</span> GMT<span class="sh_symbol">,</span>
  ctime<span class="sh_symbol">:</span> Mon<span class="sh_symbol">,</span> <span class="sh_number">10</span> Oct <span class="sh_number">2011</span> <span class="sh_number">23</span><span class="sh_symbol">:</span><span class="sh_number">24</span><span class="sh_symbol">:</span><span class="sh_number">11</span> GMT<span class="sh_symbol">,</span>
  birthtime<span class="sh_symbol">:</span> Mon<span class="sh_symbol">,</span> <span class="sh_number">10</span> Oct <span class="sh_number">2011</span> <span class="sh_number">23</span><span class="sh_symbol">:</span><span class="sh_number">24</span><span class="sh_symbol">:</span><span class="sh_number">11</span> GMT <span class="sh_cbracket">}</span>
</code></pre>
<h3>stats.isBlockDevice()<span><a class="mark" href="#fs_stats_isblockdevice" id="fs_stats_isblockdevice">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/stats_isblockdevice.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/ucRStq" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.1.10</span>
</div>
<ul>
<li>返回: <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a> </li>
</ul>
<p>如果 <code>fs.Stats</code> 对象描述块设备，则返回 <code>true</code>。</p>
<h3>stats.isCharacterDevice()<span><a class="mark" href="#fs_stats_ischaracterdevice" id="fs_stats_ischaracterdevice">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/stats_ischaracterdevice.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/eyxgv8" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.1.10</span>
</div>
<ul>
<li>返回: <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a></li>
</ul>
<p>如果 <code>fs.Stats</code> 对象描述字符设备，则返回 <code>true</code>。</p>
<h3>stats.isDirectory()<span><a class="mark" href="#fs_stats_isdirectory" id="fs_stats_isdirectory">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/stats_isdirectory.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/KwYefp" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.1.10</span>
</div>
<ul>
<li>返回: <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a></li>
</ul>
<p>如果 <code>fs.Stats</code> 对象描述文件系统目录，则返回 <code>true</code>。</p>
<h3>stats.isFIFO()<span><a class="mark" href="#fs_stats_isfifo" id="fs_stats_isfifo">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/stats_isfifo.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/7yfcow" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.1.10</span>
</div>
<ul>
<li>返回: <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a></li>
</ul>
<p>如果 <code>fs.Stats</code> 对象描述先进先出（FIFO）管道，则返回 <code>true</code>。</p>
<h3>stats.isFile()<span><a class="mark" href="#fs_stats_isfile" id="fs_stats_isfile">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/stats_isfile.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/Joxmu9" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.1.10</span>
</div>
<ul>
<li>返回: <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a></li>
</ul>
<p>如果 <code>fs.Stats</code> 对象描述常规文件，则返回 <code>true</code>。</p>
<h3>stats.isSocket()<span><a class="mark" href="#fs_stats_issocket" id="fs_stats_issocket">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/stats_issocket.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/cQrjf3" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.1.10</span>
</div>
<ul>
<li>返回: <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a></li>
</ul>
<p>如果 <code>fs.Stats</code> 对象描述套接字，则返回 <code>true</code>。</p>
<h3>stats.isSymbolicLink()<span><a class="mark" href="#fs_stats_issymboliclink" id="fs_stats_issymboliclink">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/stats_issymboliclink.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/K5dgkQ" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.1.10</span>
</div>
<ul>
<li>返回: <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a></li>
</ul>
<p>如果 <code>fs.Stats</code> 对象描述符号链接，则返回 <code>true</code>。</p>
<p>此方法仅在使用 <a href="http://nodejs.cn/s/JqRZuK" rel="nofollow" target="_blank"><code>fs.lstat()</code></a> 时有效。</p>
<h3>stats.dev<span><a class="mark" href="#fs_stats_dev" id="fs_stats_dev">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/stats_dev.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/weFX4t" class="tip_en">提交修改</a></p>
<ul>
<li><a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> | <a href="http://nodejs.cn/s/gJMq1y" target="_blank" rel="nofollow" class="type">&lt;bigint&gt;</a> </li>
</ul>
<p>包含该文件的设备的数字标识符。</p>
<h3>stats.ino<span><a class="mark" href="#fs_stats_ino" id="fs_stats_ino">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/stats_ino.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/GNKsyL" class="tip_en">提交修改</a></p>
<ul>
<li><a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> | <a href="http://nodejs.cn/s/gJMq1y" target="_blank" rel="nofollow" class="type">&lt;bigint&gt;</a> </li>
</ul>
<p>文件系统特定的文件索引节点编号。</p>
<h3>stats.mode<span><a class="mark" href="#fs_stats_mode" id="fs_stats_mode">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/stats_mode.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/ww1T8u" class="tip_en">提交修改</a></p>
<ul>
<li><a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> | <a href="http://nodejs.cn/s/gJMq1y" target="_blank" rel="nofollow" class="type">&lt;bigint&gt;</a> </li>
</ul>
<p>描述文件类型和模式的位字段。</p>
<h3>stats.nlink<span><a class="mark" href="#fs_stats_nlink" id="fs_stats_nlink">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/stats_nlink.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/X9UAcg" class="tip_en">提交修改</a></p>
<ul>
<li><a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> | <a href="http://nodejs.cn/s/gJMq1y" target="_blank" rel="nofollow" class="type">&lt;bigint&gt;</a></li>
</ul>
<p>文件存在的硬链接数。</p>
<h3>stats.uid<span><a class="mark" href="#fs_stats_uid" id="fs_stats_uid">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/stats_uid.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/bu7y4m" class="tip_en">提交修改</a></p>
<ul>
<li><a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> | <a href="http://nodejs.cn/s/gJMq1y" target="_blank" rel="nofollow" class="type">&lt;bigint&gt;</a></li>
</ul>
<p>拥有该文件（POSIX）的用户的数字型用户标识符。</p>
<h3>stats.gid<span><a class="mark" href="#fs_stats_gid" id="fs_stats_gid">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/stats_gid.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/HAyqQ1" class="tip_en">提交修改</a></p>
<ul>
<li><a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> | <a href="http://nodejs.cn/s/gJMq1y" target="_blank" rel="nofollow" class="type">&lt;bigint&gt;</a></li>
</ul>
<p>拥有该文件（POSIX）的群组的数字型群组标识符。</p>
<h3>stats.rdev<span><a class="mark" href="#fs_stats_rdev" id="fs_stats_rdev">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/stats_rdev.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/tcPDwr" class="tip_en">提交修改</a></p>
<ul>
<li><a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> | <a href="http://nodejs.cn/s/gJMq1y" target="_blank" rel="nofollow" class="type">&lt;bigint&gt;</a></li>
</ul>
<p>如果文件被视为特殊文件，则此值为数字型设备标识符。</p>
<h3>stats.size<span><a class="mark" href="#fs_stats_size" id="fs_stats_size">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/stats_size.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/RcJHb9" class="tip_en">提交修改</a></p>
<ul>
<li><a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> | <a href="http://nodejs.cn/s/gJMq1y" target="_blank" rel="nofollow" class="type">&lt;bigint&gt;</a></li>
</ul>
<p>文件的大小（以字节为单位）。</p>
<h3>stats.blksize<span><a class="mark" href="#fs_stats_blksize" id="fs_stats_blksize">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/stats_blksize.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/HSyngj" class="tip_en">提交修改</a></p>
<ul>
<li><a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> | <a href="http://nodejs.cn/s/gJMq1y" target="_blank" rel="nofollow" class="type">&lt;bigint&gt;</a></li>
</ul>
<p>用于 I/O 操作的文件系统块的大小。</p>
<h3>stats.blocks<span><a class="mark" href="#fs_stats_blocks" id="fs_stats_blocks">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/stats_blocks.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/FQAQNv" class="tip_en">提交修改</a></p>
<ul>
<li><a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> | <a href="http://nodejs.cn/s/gJMq1y" target="_blank" rel="nofollow" class="type">&lt;bigint&gt;</a></li>
</ul>
<p>为此文件分配的块数。</p>
<h3>stats.atimeMs<span><a class="mark" href="#fs_stats_atimems" id="fs_stats_atimems">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/stats_atimems.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/tErCmN" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v8.1.0</span>
</div>
<ul>
<li><a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> | <a href="http://nodejs.cn/s/gJMq1y" target="_blank" rel="nofollow" class="type">&lt;bigint&gt;</a></li>
</ul>
<p>表明上次访问此文件的时间戳，以 POSIX 纪元以来的毫秒数表示。</p>
<h3>stats.mtimeMs<span><a class="mark" href="#fs_stats_mtimems" id="fs_stats_mtimems">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/stats_mtimems.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/Sh4HE1" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v8.1.0</span>
</div>
<ul>
<li><a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> | <a href="http://nodejs.cn/s/gJMq1y" target="_blank" rel="nofollow" class="type">&lt;bigint&gt;</a></li>
</ul>
<p>表明上次修改此文件的时间戳，以 POSIX 纪元以来的毫秒数表示。</p>
<h3>stats.ctimeMs<span><a class="mark" href="#fs_stats_ctimems" id="fs_stats_ctimems">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/stats_ctimems.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/vzRG9L" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v8.1.0</span>
</div>
<ul>
<li><a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> | <a href="http://nodejs.cn/s/gJMq1y" target="_blank" rel="nofollow" class="type">&lt;bigint&gt;</a> </li>
</ul>
<p>表明上次更改文件状态的时间戳，以 POSIX 纪元以来的毫秒数表示。</p>
<h3>stats.birthtimeMs<span><a class="mark" href="#fs_stats_birthtimems" id="fs_stats_birthtimems">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/stats_birthtimems.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/6fxGKg" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v8.1.0</span>
</div>
<ul>
<li><a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> | <a href="http://nodejs.cn/s/gJMq1y" target="_blank" rel="nofollow" class="type">&lt;bigint&gt;</a></li>
</ul>
<p>表明此文件的创建时间的时间戳，以 POSIX 纪元以来的毫秒数表示。</p>
<h3>stats.atime<span><a class="mark" href="#fs_stats_atime" id="fs_stats_atime">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/stats_atime.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/fuZPmt" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.11.13</span>
</div>
<ul>
<li><a href="http://nodejs.cn/s/A9TMoa" target="_blank" rel="nofollow" class="type">&lt;Date&gt;</a> </li>
</ul>
<p>表明上次访问此文件的时间戳。</p>
<h3>stats.mtime<span><a class="mark" href="#fs_stats_mtime" id="fs_stats_mtime">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/stats_mtime.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/aXa9AY" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.11.13</span>
</div>
<ul>
<li><a href="http://nodejs.cn/s/A9TMoa" target="_blank" rel="nofollow" class="type">&lt;Date&gt;</a> </li>
</ul>
<p>表明上次修改此文件的时间戳。</p>
<h3>stats.ctime<span><a class="mark" href="#fs_stats_ctime" id="fs_stats_ctime">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/stats_ctime.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/zxqadW" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.11.13</span>
</div>
<ul>
<li><a href="http://nodejs.cn/s/A9TMoa" target="_blank" rel="nofollow" class="type">&lt;Date&gt;</a> </li>
</ul>
<p>表明上次更改文件状态的时间戳。</p>
<h3>stats.birthtime<span><a class="mark" href="#fs_stats_birthtime" id="fs_stats_birthtime">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/stats_birthtime.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/6ZhTkQ" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.11.13</span>
</div>
<ul>
<li><a href="http://nodejs.cn/s/A9TMoa" target="_blank" rel="nofollow" class="type">&lt;Date&gt;</a></li>
</ul>
<p>表示此文件的创建时间的时间戳。</p>
<h3>文件属性的时间值<span><a class="mark" href="#fs_stat_time_values" id="fs_stat_time_values">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/stat_time_values.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/hH14iP" class="tip_en">提交修改</a></p>
<p><code>atimeMs</code>、 <code>mtimeMs</code>、 <code>ctimeMs</code> 和 <code>birthtimeMs</code> 属性是保存相应时间（以毫秒为单位）的<a href="http://nodejs.cn/s/SXbo1v" rel="nofollow" target="_blank">数值</a>。
它们的精度取决于平台。
<code>atime</code>、 <code>mtime</code>、 <code>ctime</code> 和 <code>birthtime</code> 是对应时间的 <a href="http://nodejs.cn/s/A9TMoa" rel="nofollow" target="_blank"><code>Date</code></a> 对象。
<code>Date</code> 值和数值没有关联性。
赋值新的数值、或者改变 <code>Date</code> 值，都不会影响到对应的属性。</p>
<p>stat 对象中的时间具有以下语义：</p>
<ul>
<li><code>atime</code> "访问时间" - 上次访问文件数据的时间。由 <a href="http://nodejs.cn/s/dC6BPb" rel="nofollow" target="_blank"><code>mknod(2)</code></a>、 <a href="http://nodejs.cn/s/58Fxaj" rel="nofollow" target="_blank"><code>utimes(2)</code></a> 和 <a href="http://nodejs.cn/s/3BmXqe" rel="nofollow" target="_blank"><code>read(2)</code></a> 系统调用更改。</li>
<li><code>mtime</code> "修改时间" - 上次修改文件数据的时间。由 <a href="http://nodejs.cn/s/dC6BPb" rel="nofollow" target="_blank"><code>mknod(2)</code></a>、 <a href="http://nodejs.cn/s/58Fxaj" rel="nofollow" target="_blank"><code>utimes(2)</code></a> 和 <a href="http://nodejs.cn/s/NmUvmK" rel="nofollow" target="_blank"><code>write(2)</code></a> 系统调用更改。</li>
<li><code>ctime</code> "更改时间" - 上次更改文件状态（修改索引节点数据）的时间。由 <a href="http://nodejs.cn/s/K3psEw" rel="nofollow" target="_blank"><code>chmod(2)</code></a>、 <a href="http://nodejs.cn/s/vSBegL" rel="nofollow" target="_blank"><code>chown(2)</code></a>、 <a href="http://nodejs.cn/s/U8H1mr" rel="nofollow" target="_blank"><code>link(2)</code></a>、 <a href="http://nodejs.cn/s/dC6BPb" rel="nofollow" target="_blank"><code>mknod(2)</code></a>、 <a href="http://nodejs.cn/s/YbqghQ" rel="nofollow" target="_blank"><code>rename(2)</code></a>、 <a href="http://nodejs.cn/s/gjRRsM" rel="nofollow" target="_blank"><code>unlink(2)</code></a>、 <a href="http://nodejs.cn/s/58Fxaj" rel="nofollow" target="_blank"><code>utimes(2)</code></a>、 <a href="http://nodejs.cn/s/3BmXqe" rel="nofollow" target="_blank"><code>read(2)</code></a> 和 <a href="http://nodejs.cn/s/NmUvmK" rel="nofollow" target="_blank"><code>write(2)</code></a> 系统调用更改。</li>
<li><code>birthtime</code> "创建时间" - 创建文件的时间。当创建文件时设置一次。
在不支持创建时间的文件系统上，该字段可能改为保存 <code>ctime</code> 或 <code>1970-01-01T00:00Z</code>（即 Unix 纪元时间戳 <code>0</code>）。
在这种情况下，该值可能大于 <code>atime</code> 或 <code>mtime</code>。
在 Darwin 和其他的 FreeBSD 衍生系统上，也可能使用 <a href="http://nodejs.cn/s/58Fxaj" rel="nofollow" target="_blank"><code>utimes(2)</code></a> 系统调用将 <code>atime</code> 显式地设置为比 <code>birthtime</code> 更早的值。</li>
</ul>
<p>在 Node.js 0.12 之前， <code>ctime</code> 在 Windows 上保存 <code>birthtime</code>。
从 0.12 开始， <code>ctime</code> 不再是“创建时间”，而在 Unix 系统上则从来都不是。</p>
<h2>fs.WriteStream 类<span><a class="mark" href="#fs_class_fs_writestream" id="fs_class_fs_writestream">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/class_fs_writestream.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/PpP39H" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.1.93</span>
</div>
<p><code>WriteStream</code> 是一个<a href="http://nodejs.cn/s/9JUnJ8" rel="nofollow" target="_blank">可写流</a>。</p>
<h3>'close' 事件<span><a class="mark" href="#fs_event_close_2" id="fs_event_close_2">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/event_close_2.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/4LkLF2" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.1.93</span>
</div>
<p>当 <code>WriteStream</code> 的底层文件描述符已关闭时触发。</p>
<h3>'open' 事件<span><a class="mark" href="#fs_event_open_1" id="fs_event_open_1">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/event_open_1.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/G4Tt1c" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.1.93</span>
</div>
<ul>
<li><code>fd</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <code>WriteStream</code> 使用的整数型文件描述符。</li>
</ul>
<p>当 <code>WriteStream</code> 的文件打开时触发。</p>
<h3>'ready' 事件<span><a class="mark" href="#fs_event_ready_1" id="fs_event_ready_1">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/event_ready_1.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/4LXj23" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v9.11.0</span>
</div>
<p>当 <code>fs.WriteStream</code> 准备好使用时触发。</p>
<p><code>'open'</code> 事件之后立即触发。</p>
<h3>writeStream.bytesWritten<span><a class="mark" href="#fs_writestream_byteswritten" id="fs_writestream_byteswritten">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/writestream_byteswritten.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/eTUPvx" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.4.7</span>
</div>
<p>到目前为止写入的字节数。
不包括仍在排队等待写入的数据。</p>
<h3>writeStream.path<span><a class="mark" href="#fs_writestream_path" id="fs_writestream_path">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/writestream_path.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/9LKP9S" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.1.93</span>
</div>
<p>流正在写入的文件的路径，由 <a href="http://nodejs.cn/s/VdSJQa" rel="nofollow" target="_blank"><code>fs.createWriteStream()</code></a> 的第一个参数指定。
如果 <code>path</code> 传入字符串，则 <code>writeStream.path</code> 将是字符串。
如果 <code>path</code> 传入 <code>Buffer</code>，则 <code>writeStream.path</code> 将是 <code>Buffer</code>。</p>
<h2>fs.access(path[, mode], callback)<span><a class="mark" href="#fs_fs_access_path_mode_callback" id="fs_fs_access_path_mode_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_access_path_mode_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/ZKqPYh" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v6.3.0</td>
<td><p>The constants like <code>fs.R_OK</code>, etc which were present directly on <code>fs</code> were moved into <code>fs.constants</code> as a soft deprecation. Thus for Node.js <code>&lt; v6.3.0</code> use <code>fs</code> to access those constants, or do something like <code>(fs.constants || fs).R_OK</code> to work with all versions.</p></td></tr>
<tr><td>v0.11.15</td>
<td><p><span>新增于: v0.11.15</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>mode</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <strong>默认值:</strong> <code>fs.constants.F_OK</code>。</li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
</ul>
</li>
</ul>
<p>测试用户对 <code>path</code> 指定的文件或目录的权限。
<code>mode</code> 参数是一个可选的整数，指定要执行的可访问性检查。
<code>mode</code> 可选的值参阅<a href="http://nodejs.cn/s/qZfpqk" rel="nofollow" target="_blank">文件可访问性的常量</a>。
可以创建由两个或更多个值按位或组成的掩码（例如 <code>fs.constants.W_OK | fs.constants.R_OK</code>）。</p>
<p>最后一个参数 <code>callback</code> 是一个回调函数，调用时将传入可能的错误参数。
如果可访问性检查失败，则错误参数将是 <code>Error</code> 对象。
以下示例检查 <code>package.json</code> 是否存在，以及它是否可读或可写。</p>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_keyword">const</span> file <span class="sh_symbol">=</span> <span class="sh_string">'package.json'</span><span class="sh_symbol">;</span>

<span class="sh_comment">// 检查当前目录中是否存在该文件。</span>
fs<span class="sh_symbol">.</span><span class="sh_function">access</span><span class="sh_symbol">(</span>file<span class="sh_symbol">,</span> fs<span class="sh_symbol">.</span>constants<span class="sh_symbol">.</span>F_OK<span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span>`$<span class="sh_cbracket">{</span>file<span class="sh_cbracket">}</span> $<span class="sh_cbracket">{</span>err <span class="sh_symbol">?</span> <span class="sh_string">'不存在'</span> <span class="sh_symbol">:</span> <span class="sh_string">'存在'</span><span class="sh_cbracket">}</span>`<span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>

<span class="sh_comment">// 检查文件是否可读。</span>
fs<span class="sh_symbol">.</span><span class="sh_function">access</span><span class="sh_symbol">(</span>file<span class="sh_symbol">,</span> fs<span class="sh_symbol">.</span>constants<span class="sh_symbol">.</span>R_OK<span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span>`$<span class="sh_cbracket">{</span>file<span class="sh_cbracket">}</span> $<span class="sh_cbracket">{</span>err <span class="sh_symbol">?</span> <span class="sh_string">'不可读'</span> <span class="sh_symbol">:</span> <span class="sh_string">'可读'</span><span class="sh_cbracket">}</span>`<span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>

<span class="sh_comment">// 检查文件是否可写。</span>
fs<span class="sh_symbol">.</span><span class="sh_function">access</span><span class="sh_symbol">(</span>file<span class="sh_symbol">,</span> fs<span class="sh_symbol">.</span>constants<span class="sh_symbol">.</span>W_OK<span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span>`$<span class="sh_cbracket">{</span>file<span class="sh_cbracket">}</span> $<span class="sh_cbracket">{</span>err <span class="sh_symbol">?</span> <span class="sh_string">'不可写'</span> <span class="sh_symbol">:</span> <span class="sh_string">'可写'</span><span class="sh_cbracket">}</span>`<span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>

<span class="sh_comment">// 检查当前目录中是否存在该文件，以及该文件是否可写。</span>
fs<span class="sh_symbol">.</span><span class="sh_function">access</span><span class="sh_symbol">(</span>file<span class="sh_symbol">,</span> fs<span class="sh_symbol">.</span>constants<span class="sh_symbol">.</span>F_OK <span class="sh_symbol">|</span> fs<span class="sh_symbol">.</span>constants<span class="sh_symbol">.</span>W_OK<span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span>
    console<span class="sh_symbol">.</span><span class="sh_function">error</span><span class="sh_symbol">(</span>
      `$<span class="sh_cbracket">{</span>file<span class="sh_cbracket">}</span> $<span class="sh_cbracket">{</span>err<span class="sh_symbol">.</span>code <span class="sh_symbol">===</span> <span class="sh_string">'ENOENT'</span> <span class="sh_symbol">?</span> <span class="sh_string">'不存在'</span> <span class="sh_symbol">:</span> <span class="sh_string">'只可读'</span><span class="sh_cbracket">}</span>`<span class="sh_symbol">);</span>
  <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_cbracket">{</span>
    console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span>`$<span class="sh_cbracket">{</span>file<span class="sh_cbracket">}</span> 存在，且它是可写的`<span class="sh_symbol">);</span>
  <span class="sh_cbracket">}</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<p>不建议在调用 <code>fs.open()</code>、 <code>fs.readFile()</code> 或 <code>fs.writeFile()</code> 之前使用 <code>fs.access()</code> 检查文件的可访问性。
这样做会引入竞态条件，因为其他进程可能会在两个调用之间更改文件的状态。
相反，应该直接打开、读取或写入文件，如果文件无法访问则处理引发的错误。</p>
<p><strong>写入（不推荐）</strong></p>
<pre class="sh_sourceCode"><code class="language-js">fs<span class="sh_symbol">.</span><span class="sh_function">access</span><span class="sh_symbol">(</span><span class="sh_string">'myfile'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(!</span>err<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span>
    console<span class="sh_symbol">.</span><span class="sh_function">error</span><span class="sh_symbol">(</span><span class="sh_string">'myfile 已存在'</span><span class="sh_symbol">);</span>
    <span class="sh_keyword">return</span><span class="sh_symbol">;</span>
  <span class="sh_cbracket">}</span>

  fs<span class="sh_symbol">.</span><span class="sh_function">open</span><span class="sh_symbol">(</span><span class="sh_string">'myfile'</span><span class="sh_symbol">,</span> <span class="sh_string">'wx'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">,</span> fd<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
    <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
    <span class="sh_function">writeMyData</span><span class="sh_symbol">(</span>fd<span class="sh_symbol">);</span>
  <span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<p><strong>写入（推荐）</strong></p>
<pre class="sh_sourceCode"><code class="language-js">fs<span class="sh_symbol">.</span><span class="sh_function">open</span><span class="sh_symbol">(</span><span class="sh_string">'myfile'</span><span class="sh_symbol">,</span> <span class="sh_string">'wx'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">,</span> fd<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span>
    <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">.</span>code <span class="sh_symbol">===</span> <span class="sh_string">'EEXIST'</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span>
      console<span class="sh_symbol">.</span><span class="sh_function">error</span><span class="sh_symbol">(</span><span class="sh_string">'myfile 已存在'</span><span class="sh_symbol">);</span>
      <span class="sh_keyword">return</span><span class="sh_symbol">;</span>
    <span class="sh_cbracket">}</span>

    <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
  <span class="sh_cbracket">}</span>

  <span class="sh_function">writeMyData</span><span class="sh_symbol">(</span>fd<span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<p><strong>读取（不推荐）</strong></p>
<pre class="sh_sourceCode"><code class="language-js">fs<span class="sh_symbol">.</span><span class="sh_function">access</span><span class="sh_symbol">(</span><span class="sh_string">'myfile'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span>
    <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">.</span>code <span class="sh_symbol">===</span> <span class="sh_string">'ENOENT'</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span>
      console<span class="sh_symbol">.</span><span class="sh_function">error</span><span class="sh_symbol">(</span><span class="sh_string">'myfile 不存在'</span><span class="sh_symbol">);</span>
      <span class="sh_keyword">return</span><span class="sh_symbol">;</span>
    <span class="sh_cbracket">}</span>

    <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
  <span class="sh_cbracket">}</span>

  fs<span class="sh_symbol">.</span><span class="sh_function">open</span><span class="sh_symbol">(</span><span class="sh_string">'myfile'</span><span class="sh_symbol">,</span> <span class="sh_string">'r'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">,</span> fd<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
    <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
    <span class="sh_function">readMyData</span><span class="sh_symbol">(</span>fd<span class="sh_symbol">);</span>
  <span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<p><strong>读取（推荐）</strong></p>
<pre class="sh_sourceCode"><code class="language-js">fs<span class="sh_symbol">.</span><span class="sh_function">open</span><span class="sh_symbol">(</span><span class="sh_string">'myfile'</span><span class="sh_symbol">,</span> <span class="sh_string">'r'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">,</span> fd<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span>
    <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">.</span>code <span class="sh_symbol">===</span> <span class="sh_string">'ENOENT'</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span>
      console<span class="sh_symbol">.</span><span class="sh_function">error</span><span class="sh_symbol">(</span><span class="sh_string">'myfile 不存在'</span><span class="sh_symbol">);</span>
      <span class="sh_keyword">return</span><span class="sh_symbol">;</span>
    <span class="sh_cbracket">}</span>

    <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
  <span class="sh_cbracket">}</span>

  <span class="sh_function">readMyData</span><span class="sh_symbol">(</span>fd<span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<p>上面的“不推荐”示例会先检查可访问性，然后再使用文件。
“推荐”示例则更好，因为它们直接使用文件并处理错误（如果有错误的话）。</p>
<p>通常，仅在不直接使用文件时才检查文件的可访问性，例如当其可访问性是来自其他进程的信号时。</p>
<p>在 Windows 上，目录上的访问控制策略（ACL）可能会限制对文件或目录的访问。
但是， <code>fs.access()</code> 函数不检查 ACL，因此即使 ACL 限制用户读取或写入路径，也可能报告路径是可访问的。</p>
<h2>fs.accessSync(path[, mode])<span><a class="mark" href="#fs_fs_accesssync_path_mode" id="fs_fs_accesssync_path_mode">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_accesssync_path_mode.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/nVbaFv" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v0.11.15</td>
<td><p><span>新增于: v0.11.15</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>mode</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <strong>默认值:</strong> <code>fs.constants.F_OK</code>。</li>
</ul>
<p>同步地测试用户对 <code>path</code> 指定的文件或目录的权限。
<code>mode</code> 参数是一个可选的整数，指定要执行的可访问性检查。
<code>mode</code> 可选的值参阅<a href="http://nodejs.cn/s/qZfpqk" rel="nofollow" target="_blank">文件可访问性的常量</a>。
可以创建由两个或更多个值按位或组成的掩码（例如 <code>fs.constants.W_OK | fs.constants.R_OK</code>）。</p>
<p>如果可访问性检查失败，则抛出 <code>Error</code>。
否则，该方法将返回 <code>undefined</code>。</p>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_keyword">try</span> <span class="sh_cbracket">{</span>
  fs<span class="sh_symbol">.</span><span class="sh_function">accessSync</span><span class="sh_symbol">(</span><span class="sh_string">'etc/passwd'</span><span class="sh_symbol">,</span> fs<span class="sh_symbol">.</span>constants<span class="sh_symbol">.</span>R_OK <span class="sh_symbol">|</span> fs<span class="sh_symbol">.</span>constants<span class="sh_symbol">.</span>W_OK<span class="sh_symbol">);</span>
  console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span><span class="sh_string">'可以读写'</span><span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span> <span class="sh_keyword">catch</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span>
  console<span class="sh_symbol">.</span><span class="sh_function">error</span><span class="sh_symbol">(</span><span class="sh_string">'无权访问'</span><span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span>
</code></pre>
<h2>fs.appendFile(path, data[, options], callback)<span><a class="mark" href="#fs_fs_appendfile_path_data_options_callback" id="fs_fs_appendfile_path_data_options_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_appendfile_path_data_options_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/zXYYUf" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The passed <code>options</code> object will never be modified.</p></td></tr>
<tr><td>v5.0.0</td>
<td><p>The <code>file</code> parameter can be a file descriptor now.</p></td></tr>
<tr><td>v0.6.7</td>
<td><p><span>新增于: v0.6.7</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a> | <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> 文件名或文件描述符。</li>
<li><code>data</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a> | <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a></p>
<ul>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/334hvC" target="_blank" rel="nofollow" class="type">&lt;null&gt;</a> <strong>默认值:</strong> <code>'utf8'</code>。</li>
<li><code>mode</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <strong>默认值:</strong> <code>0o666</code>。</li>
<li><code>flag</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> 参阅<a href="http://nodejs.cn/s/JjbY8n" rel="nofollow" target="_blank">支持的文件系统标志</a>。<strong>默认值:</strong> <code>'a'</code>。</li>
</ul>
</li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
</ul>
</li>
</ul>
<p>异步地将数据追加到文件，如果文件尚不存在则创建该文件。
<code>data</code> 可以是字符串或 <a href="http://nodejs.cn/s/FApxjh" rel="nofollow" target="_blank"><code>Buffer</code></a>。</p>
<pre class="sh_sourceCode"><code class="language-js">fs<span class="sh_symbol">.</span><span class="sh_function">appendFile</span><span class="sh_symbol">(</span><span class="sh_string">'message.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'追加的数据'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
  console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span><span class="sh_string">'数据已追加到文件'</span><span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<p>如果 <code>options</code> 是字符串，则它指定字符编码：</p>
<pre class="sh_sourceCode"><code class="language-js">fs<span class="sh_symbol">.</span><span class="sh_function">appendFile</span><span class="sh_symbol">(</span><span class="sh_string">'message.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'追加的数据'</span><span class="sh_symbol">,</span> <span class="sh_string">'utf8'</span><span class="sh_symbol">,</span> callback<span class="sh_symbol">);</span>
</code></pre>
<p><code>path</code> 可以指定为已打开用于追加（使用 <code>fs.open()</code> 或 <code>fs.openSync()</code>）的数字型文件描述符。
文件描述符不会自动关闭。</p>
<pre class="sh_sourceCode"><code class="language-js">fs<span class="sh_symbol">.</span><span class="sh_function">open</span><span class="sh_symbol">(</span><span class="sh_string">'message.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'a'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">,</span> fd<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
  fs<span class="sh_symbol">.</span><span class="sh_function">appendFile</span><span class="sh_symbol">(</span>fd<span class="sh_symbol">,</span> <span class="sh_string">'追加的数据'</span><span class="sh_symbol">,</span> <span class="sh_string">'utf8'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
    fs<span class="sh_symbol">.</span><span class="sh_function">close</span><span class="sh_symbol">(</span>fd<span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
      <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
    <span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
    <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
  <span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<h2>fs.appendFileSync(path, data[, options])<span><a class="mark" href="#fs_fs_appendfilesync_path_data_options" id="fs_fs_appendfilesync_path_data_options">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_appendfilesync_path_data_options.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/k6Wt3R" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v7.0.0</td>
<td><p>The passed <code>options</code> object will never be modified.</p></td></tr>
<tr><td>v5.0.0</td>
<td><p>The <code>file</code> parameter can be a file descriptor now.</p></td></tr>
<tr><td>v0.6.7</td>
<td><p><span>新增于: v0.6.7</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a> | <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> 文件名或文件描述符。</li>
<li><code>data</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a> | <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a></p>
<ul>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/334hvC" target="_blank" rel="nofollow" class="type">&lt;null&gt;</a> <strong>默认值:</strong> <code>'utf8'</code>。</li>
<li><code>mode</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <strong>默认值:</strong> <code>0o666</code>。</li>
<li><code>flag</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> 参阅<a href="http://nodejs.cn/s/JjbY8n" rel="nofollow" target="_blank">支持的文件系统标志</a>。<strong>默认值:</strong> <code>'a'</code>。</li>
</ul>
</li>
</ul>
<p>同步地将数据追加到文件，如果文件尚不存在则创建该文件。
<code>data</code> 可以是字符串或 <a href="http://nodejs.cn/s/FApxjh" rel="nofollow" target="_blank"><code>Buffer</code></a>。</p>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_keyword">try</span> <span class="sh_cbracket">{</span>
  fs<span class="sh_symbol">.</span><span class="sh_function">appendFileSync</span><span class="sh_symbol">(</span><span class="sh_string">'message.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'追加的数据'</span><span class="sh_symbol">);</span>
  console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span><span class="sh_string">'数据已追加到文件'</span><span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span> <span class="sh_keyword">catch</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span>
  <span class="sh_comment">/* 处理错误 */</span>
<span class="sh_cbracket">}</span>
</code></pre>
<p>如果 <code>options</code> 是字符串，则它指定字符编码：</p>
<pre class="sh_sourceCode"><code class="language-js">fs<span class="sh_symbol">.</span><span class="sh_function">appendFileSync</span><span class="sh_symbol">(</span><span class="sh_string">'message.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'追加的数据'</span><span class="sh_symbol">,</span> <span class="sh_string">'utf8'</span><span class="sh_symbol">);</span>
</code></pre>
<p><code>path</code> 可以指定为已打开用于追加（使用 <code>fs.open()</code> 或 <code>fs.openSync()</code>）的数字型文件描述符。
文件描述符不会自动关闭。</p>
<pre class="sh_sourceCode"><code class="language-js">let fd<span class="sh_symbol">;</span>

<span class="sh_keyword">try</span> <span class="sh_cbracket">{</span>
  fd <span class="sh_symbol">=</span> fs<span class="sh_symbol">.</span><span class="sh_function">openSync</span><span class="sh_symbol">(</span><span class="sh_string">'message.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'a'</span><span class="sh_symbol">);</span>
  fs<span class="sh_symbol">.</span><span class="sh_function">appendFileSync</span><span class="sh_symbol">(</span>fd<span class="sh_symbol">,</span> <span class="sh_string">'追加的数据'</span><span class="sh_symbol">,</span> <span class="sh_string">'utf8'</span><span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span> <span class="sh_keyword">catch</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span>
  <span class="sh_comment">/* 处理错误 */</span>
<span class="sh_cbracket">}</span> <span class="sh_keyword">finally</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>fd <span class="sh_symbol">!==</span> <span class="sh_predef_var">undefined</span><span class="sh_symbol">)</span>
    fs<span class="sh_symbol">.</span><span class="sh_function">closeSync</span><span class="sh_symbol">(</span>fd<span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span>
</code></pre>
<h2>fs.chmod(path, mode, callback)<span><a class="mark" href="#fs_fs_chmod_path_mode_callback" id="fs_fs_chmod_path_mode_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_chmod_path_mode_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/Rfa8zU" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.1.30</td>
<td><p><span>新增于: v0.1.30</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>mode</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
</ul>
</li>
</ul>
<p>异步地更改文件的权限。
除了可能的异常，完成回调没有其他参数。</p>
<p>也可参阅 <a href="http://nodejs.cn/s/K3psEw" rel="nofollow" target="_blank"><code>chmod(2)</code></a>。</p>
<h3>文件的模式<span><a class="mark" href="#fs_file_modes" id="fs_file_modes">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/file_modes.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/8FEWfT" class="tip_en">提交修改</a></p>
<p><code>fs.chmod()</code> 和 <code>fs.chmodSync()</code> 方法中使用的 <code>mode</code> 参数是使用以下常量的逻辑或运算创建的数字型位掩码：</p>























































<table><thead><tr><th>常量</th><th>八进制值</th><th>说明</th></tr></thead><tbody><tr><td><code>fs.constants.S_IRUSR</code></td><td><code>0o400</code></td><td>所有者可读</td></tr><tr><td><code>fs.constants.S_IWUSR</code></td><td><code>0o200</code></td><td>所有者可写</td></tr><tr><td><code>fs.constants.S_IXUSR</code></td><td><code>0o100</code></td><td>所有者可执行或搜索</td></tr><tr><td><code>fs.constants.S_IRGRP</code></td><td><code>0o40</code></td><td>群组可读</td></tr><tr><td><code>fs.constants.S_IWGRP</code></td><td><code>0o20</code></td><td>群组可写</td></tr><tr><td><code>fs.constants.S_IXGRP</code></td><td><code>0o10</code></td><td>群组可执行或搜索</td></tr><tr><td><code>fs.constants.S_IROTH</code></td><td><code>0o4</code></td><td>其他人可读</td></tr><tr><td><code>fs.constants.S_IWOTH</code></td><td><code>0o2</code></td><td>其他人可写</td></tr><tr><td><code>fs.constants.S_IXOTH</code></td><td><code>0o1</code></td><td>其他人可执行或搜索</td></tr></tbody></table>
<p>构造 <code>mode</code> 更简单的方法是使用三个八进制数字的序列（ 例如 <code>765</code>）。
最左边的数字（示例中的 <code>7</code>）指定文件所有者的权限。
中间的数字（示例中的 <code>6</code>）指定群组的权限。
最右边的数字（示例中的 <code>5</code>）指定其他人的权限。</p>









































<table><thead><tr><th>数字</th><th>说明</th></tr></thead><tbody><tr><td><code>7</code></td><td>可读、可写、可执行</td></tr><tr><td><code>6</code></td><td>可读、可写</td></tr><tr><td><code>5</code></td><td>可读、可执行</td></tr><tr><td><code>4</code></td><td>只读</td></tr><tr><td><code>3</code></td><td>可写、可执行</td></tr><tr><td><code>2</code></td><td>只写</td></tr><tr><td><code>1</code></td><td>只可执行</td></tr><tr><td><code>0</code></td><td>没有权限</td></tr></tbody></table>
<p>例如，八进制值 <code>0o765</code> 表示：</p>
<ul>
<li>所有者可以读取、写入和执行该文件。</li>
<li>群组可以读和写入该文件。</li>
<li>其他人可以读取和执行该文件。</li>
</ul>
<p>当使用期望的文件模式的原始数字时，任何大于 <code>0o777</code> 的值都可能导致不支持一致的特定于平台的行为。
因此，诸如 <code>S_ISVTX</code>、 <code>S_ISGID</code> 或 <code>S_ISUID</code> 之类的常量不会在 <code>fs.constants</code> 中公开。</p>
<p>注意事项：在 Windows 上，只能更改写入权限，并且不会实现群组、所有者或其他人的权限之间的区别。</p>
<h2>fs.chmodSync(path, mode)<span><a class="mark" href="#fs_fs_chmodsync_path_mode" id="fs_fs_chmodsync_path_mode">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_chmodsync_path_mode.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/sAZw3T" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v0.6.7</td>
<td><p><span>新增于: v0.6.7</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>mode</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
</ul>
<p>有关详细信息，参阅此 API 的异步版本的文档：<a href="http://nodejs.cn/s/6QUJs8" rel="nofollow" target="_blank"><code>fs.chmod()</code></a>。</p>
<p>也可参阅 <a href="http://nodejs.cn/s/K3psEw" rel="nofollow" target="_blank"><code>chmod(2)</code></a>。</p>
<h2>fs.chown(path, uid, gid, callback)<span><a class="mark" href="#fs_fs_chown_path_uid_gid_callback" id="fs_fs_chown_path_uid_gid_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_chown_path_uid_gid_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/EzCQ6Z" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.1.97</td>
<td><p><span>新增于: v0.1.97</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>uid</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>gid</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
</ul>
</li>
</ul>
<p>异步地更改文件的所有者和群组。
除了可能的异常，完成回调没有其他参数。</p>
<p>也可参阅 <a href="http://nodejs.cn/s/vSBegL" rel="nofollow" target="_blank"><code>chown(2)</code></a>。</p>
<h2>fs.chownSync(path, uid, gid)<span><a class="mark" href="#fs_fs_chownsync_path_uid_gid" id="fs_fs_chownsync_path_uid_gid">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_chownsync_path_uid_gid.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/8CJLRT" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v0.1.97</td>
<td><p><span>新增于: v0.1.97</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>uid</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>gid</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
</ul>
<p>同步地更改文件的所有者和群组。
返回 <code>undefined</code>。
这是 <a href="http://nodejs.cn/s/WXjM7j" rel="nofollow" target="_blank"><code>fs.chown()</code></a> 的同步版本。</p>
<p>也可参阅 <a href="http://nodejs.cn/s/vSBegL" rel="nofollow" target="_blank"><code>chown(2)</code></a>。</p>
<h2>fs.close(fd, callback)<span><a class="mark" href="#fs_fs_close_fd_callback" id="fs_fs_close_fd_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_close_fd_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/j9a5Fm" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.0.2</td>
<td><p><span>新增于: v0.0.2</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
</ul>
</li>
</ul>
<p>异步的 <a href="http://nodejs.cn/s/vCJCMG" rel="nofollow" target="_blank"><code>close(2)</code></a>。
除了可能的异常，完成回调没有其他参数。</p>
<h2>fs.closeSync(fd)<span><a class="mark" href="#fs_fs_closesync_fd" id="fs_fs_closesync_fd">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_closesync_fd.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/NGc9Hw" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.1.21</span>
</div>
<ul>
<li><code>fd</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
</ul>
<p>同步的 <a href="http://nodejs.cn/s/vCJCMG" rel="nofollow" target="_blank"><code>close(2)</code></a>。返回 <code>undefined</code>。</p>
<h2>fs.constants<span><a class="mark" href="#fs_fs_constants" id="fs_fs_constants">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_constants.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/4Zevz3" class="tip_en">提交修改</a></p>
<ul>
<li><a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a></li>
</ul>
<p>返回包含文件系统操作常用常量的对象。
当前定义的特定常量在 <a href="http://nodejs.cn/s/4UdXHr" rel="nofollow" target="_blank">FS 常量</a>中描述。</p>
<h2>fs.copyFile(src, dest[, flags], callback)<span><a class="mark" href="#fs_fs_copyfile_src_dest_flags_callback" id="fs_fs_copyfile_src_dest_flags_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_copyfile_src_dest_flags_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/KGH5MR" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v8.5.0</span>
</div>
<ul>
<li><code>src</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a> 要拷贝的源文件名。</li>
<li><code>dest</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a> 拷贝操作的目标文件名。</li>
<li><code>flags</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> 用于拷贝操作的修饰符。<strong>默认值:</strong> <code>0</code>。</li>
<li><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></li>
</ul>
<p>异步地将 <code>src</code> 拷贝到 <code>dest</code>。
默认情况下，如果 <code>dest</code> 已经存在，则覆盖它。
除了可能的异常，回调函数没有其他参数。
Node.js 不保证拷贝操作的原子性。
如果在打开目标文件用于写入后发生错误，则 Node.js 将尝试删除目标文件。</p>
<p><code>flags</code> 是一个可选的整数，指定拷贝操作的行为。
可以创建由两个或更多个值按位或组成的掩码（比如 <code>fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE</code>）。</p>
<ul>
<li><code>fs.constants.COPYFILE_EXCL</code> - 如果 <code>dest</code> 已存在，则拷贝操作将失败。</li>
<li><code>fs.constants.COPYFILE_FICLONE</code> - 拷贝操作将尝试创建写时拷贝（copy-on-write）链接。如果平台不支持写时拷贝，则使用后备的拷贝机制。</li>
<li><code>fs.constants.COPYFILE_FICLONE_FORCE</code> - 拷贝操作将尝试创建写时拷贝链接。如果平台不支持写时拷贝，则拷贝操作将失败。</li>
</ul>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_keyword">const</span> fs <span class="sh_symbol">=</span> <span class="sh_function">require</span><span class="sh_symbol">(</span><span class="sh_string">'fs'</span><span class="sh_symbol">);</span>

<span class="sh_comment">// 默认情况下将创建或覆盖目标文件。</span>
fs<span class="sh_symbol">.</span><span class="sh_function">copyFile</span><span class="sh_symbol">(</span><span class="sh_string">'源文件.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'目标文件.txt'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
  console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span><span class="sh_string">'源文件已拷贝到目标文件'</span><span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<p>如果第三个参数是数字，则它指定 <code>flags</code>:  </p>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_keyword">const</span> fs <span class="sh_symbol">=</span> <span class="sh_function">require</span><span class="sh_symbol">(</span><span class="sh_string">'fs'</span><span class="sh_symbol">);</span>
<span class="sh_keyword">const</span> <span class="sh_cbracket">{</span> COPYFILE_EXCL <span class="sh_cbracket">}</span> <span class="sh_symbol">=</span> fs<span class="sh_symbol">.</span>constants<span class="sh_symbol">;</span>

<span class="sh_comment">// 通过使用 COPYFILE_EXCL，如果目标文件存在，则操作将失败。</span>
fs<span class="sh_symbol">.</span><span class="sh_function">copyFile</span><span class="sh_symbol">(</span><span class="sh_string">'源文件.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'目标文件.txt'</span><span class="sh_symbol">,</span> COPYFILE_EXCL<span class="sh_symbol">,</span> callback<span class="sh_symbol">);</span>
</code></pre>
<h2>fs.copyFileSync(src, dest[, flags])<span><a class="mark" href="#fs_fs_copyfilesync_src_dest_flags" id="fs_fs_copyfilesync_src_dest_flags">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_copyfilesync_src_dest_flags.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/G4QvaA" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v8.5.0</span>
</div>
<ul>
<li><code>src</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a> 要拷贝的源文件名。</li>
<li><code>dest</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a> 拷贝操作的目标文件名。</li>
<li><code>flags</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> 用于拷贝操作的修饰符。<strong>默认值:</strong> <code>0</code>。</li>
</ul>
<p>同步地将 <code>src</code> 拷贝到 <code>dest</code>。
默认情况下，如果 <code>dest</code> 已经存在，则覆盖它。
返回 <code>undefined</code>。
Node.js 不保证拷贝操作的原子性。
如果在打开目标文件用于写入后发生错误，则 Node.js 将尝试删除目标文件。</p>
<p><code>flags</code> 是一个可选的整数，指定拷贝操作的行为。
可以创建由两个或更多个值按位或组成的掩码（比如 <code>fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE</code>）。</p>
<ul>
<li><code>fs.constants.COPYFILE_EXCL</code> - 如果 <code>dest</code> 已存在，则拷贝操作将失败。</li>
<li><code>fs.constants.COPYFILE_FICLONE</code> - 拷贝操作将尝试创建写时拷贝（copy-on-write）链接。如果平台不支持写时拷贝，则使用后备的拷贝机制。</li>
<li><code>fs.constants.COPYFILE_FICLONE_FORCE</code> - 拷贝操作将尝试创建写时拷贝链接。如果平台不支持写时拷贝，则拷贝操作将失败。</li>
</ul>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_keyword">const</span> fs <span class="sh_symbol">=</span> <span class="sh_function">require</span><span class="sh_symbol">(</span><span class="sh_string">'fs'</span><span class="sh_symbol">);</span>

<span class="sh_comment">// 默认情况下将创建或覆盖目标文件。</span>
fs<span class="sh_symbol">.</span><span class="sh_function">copyFileSync</span><span class="sh_symbol">(</span><span class="sh_string">'源文件.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'目标文件.txt'</span><span class="sh_symbol">);</span>
console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span><span class="sh_string">'源文件已拷贝到目标文件'</span><span class="sh_symbol">);</span>
</code></pre>
<p>如果第三个参数是数字，则它指定 <code>flags</code>:  </p>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_keyword">const</span> fs <span class="sh_symbol">=</span> <span class="sh_function">require</span><span class="sh_symbol">(</span><span class="sh_string">'fs'</span><span class="sh_symbol">);</span>
<span class="sh_keyword">const</span> <span class="sh_cbracket">{</span> COPYFILE_EXCL <span class="sh_cbracket">}</span> <span class="sh_symbol">=</span> fs<span class="sh_symbol">.</span>constants<span class="sh_symbol">;</span>

<span class="sh_comment">// 通过使用 COPYFILE_EXCL，如果目标文件存在，则操作将失败。</span>
fs<span class="sh_symbol">.</span><span class="sh_function">copyFileSync</span><span class="sh_symbol">(</span><span class="sh_string">'源文件.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'目标文件.txt'</span><span class="sh_symbol">,</span> COPYFILE_EXCL<span class="sh_symbol">);</span>
</code></pre>
<h2>fs.createReadStream(path[, options])<span><a class="mark" href="#fs_fs_createreadstream_path_options" id="fs_fs_createreadstream_path_options">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_createreadstream_path_options.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/rFZn7t" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The passed <code>options</code> object will never be modified.</p></td></tr>
<tr><td>v2.3.0</td>
<td><p>The passed <code>options</code> object can be a string now.</p></td></tr>
<tr><td>v0.1.31</td>
<td><p><span>新增于: v0.1.31</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a></p>
<ul>
<li><code>flags</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> 参阅<a href="http://nodejs.cn/s/JjbY8n" rel="nofollow" target="_blank">支持的文件系统标志</a>。<strong>默认值:</strong> <code>'r'</code>。</li>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> <strong>默认值:</strong> <code>null</code>。</li>
<li><code>fd</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <strong>默认值:</strong> <code>null</code>。</li>
<li><code>mode</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <strong>默认值:</strong> <code>0o666</code>。</li>
<li><code>autoClose</code> <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a> <strong>默认值:</strong> <code>true</code>。</li>
<li><code>start</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>end</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <strong>默认值:</strong> <code>Infinity</code>。</li>
<li><code>highWaterMark</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <strong>默认值:</strong> <code>64 * 1024</code>。</li>
</ul>
</li>
<li>返回: <a href="http://nodejs.cn/s/C3Eioq" target="_blank" rel="nofollow" class="type">&lt;fs.ReadStream&gt;</a> 参阅<a href="http://nodejs.cn/s/YuDKX1" rel="nofollow" target="_blank">可读流</a>。</li>
</ul>
<p>与可读流的 16 kb 的默认 <code>highWaterMark</code> 不同，此方法返回的流具有 64 kb 的默认 <code>highWaterMark</code>。</p>
<p><code>options</code> 可以包括 <code>start</code> 和 <code>end</code> 值，以从文件中读取一定范围的字节而不是读取整个文件。
<code>start</code> 和 <code>end</code> 都包含在内并从 0 开始计数。
如果指定了 <code>fd</code> 并且 <code>start</code> 被省略或 <code>undefined</code>，则 <code>fs.createReadStream()</code> 从当前文件位置开始顺序读取。
<code>encoding</code> 可以是 <a href="http://nodejs.cn/s/FApxjh" rel="nofollow" target="_blank"><code>Buffer</code></a> 接受的任何一种字符编码。</p>
<p>如果指定了 <code>fd</code>，则 <code>ReadStream</code> 将忽略 <code>path</code> 参数并使用指定的文件描述符。
这意味着不会触发 <code>'open'</code> 事件。
<code>fd</code> 必须是阻塞的，非阻塞的 <code>fd</code> 应该传给 <a href="http://nodejs.cn/s/wsJ1o1" rel="nofollow" target="_blank"><code>net.Socket</code></a>。</p>
<p>如果 <code>fd</code> 指向仅支持阻塞读取的字符设备（例如键盘或声卡），则在数据可用之前，读取操作不会完成。
这可以防止进程退出并且流自然关闭。</p>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_keyword">const</span> fs <span class="sh_symbol">=</span> <span class="sh_function">require</span><span class="sh_symbol">(</span><span class="sh_string">'fs'</span><span class="sh_symbol">);</span>
<span class="sh_comment">// 从某个字符设备创建一个流。</span>
<span class="sh_keyword">const</span> stream <span class="sh_symbol">=</span> fs<span class="sh_symbol">.</span><span class="sh_function">createReadStream</span><span class="sh_symbol">(</span><span class="sh_string">'/dev/input/event0'</span><span class="sh_symbol">);</span>
<span class="sh_function">setTimeout</span><span class="sh_symbol">(()</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  stream<span class="sh_symbol">.</span><span class="sh_function">close</span><span class="sh_symbol">();</span> <span class="sh_comment">// 这可能不会关闭流。</span>
  <span class="sh_comment">// 人工标记流末尾，就好像底层资源本身已指示文件结尾一样，允许流关闭。</span>
  <span class="sh_comment">// 这不会取消挂起的读取操作，如果存在此类操作，则该过程可能仍无法成功退出，直到完成为止。</span>
  stream<span class="sh_symbol">.</span><span class="sh_function">push</span><span class="sh_symbol">(</span><span class="sh_keyword">null</span><span class="sh_symbol">);</span>
  stream<span class="sh_symbol">.</span><span class="sh_function">read</span><span class="sh_symbol">(</span><span class="sh_number">0</span><span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">,</span> <span class="sh_number">100</span><span class="sh_symbol">);</span>
</code></pre>
<p>如果 <code>autoClose</code> 为 <code>false</code>，则即使出现错误，也不会关闭文件描述符。
应用程序负责关闭它并确保没有文件描述符泄漏。
如果 <code>autoClose</code> 设为 <code>true</code>（默认行为），则在 <code>'error'</code> 或 <code>'end'</code> 事件时将自动关闭文件描述符。</p>
<p><code>mode</code> 用于设置文件模式（权限和粘滞位），但仅限于创建文件的情况。</p>
<p>示例，从一个大小为 100 字节的文件中读取最后 10 个字节：</p>
<pre class="sh_sourceCode"><code class="language-js">fs<span class="sh_symbol">.</span><span class="sh_function">createReadStream</span><span class="sh_symbol">(</span><span class="sh_string">'sample.txt'</span><span class="sh_symbol">,</span> <span class="sh_cbracket">{</span> start<span class="sh_symbol">:</span> <span class="sh_number">90</span><span class="sh_symbol">,</span> end<span class="sh_symbol">:</span> <span class="sh_number">99</span> <span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<p>如果 <code>options</code> 是字符串，则它指定字符编码。</p>
<h2>fs.createWriteStream(path[, options])<span><a class="mark" href="#fs_fs_createwritestream_path_options" id="fs_fs_createwritestream_path_options">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_createwritestream_path_options.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/jho1VC" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The passed <code>options</code> object will never be modified.</p></td></tr>
<tr><td>v5.5.0</td>
<td><p>The <code>autoClose</code> option is supported now.</p></td></tr>
<tr><td>v2.3.0</td>
<td><p>The passed <code>options</code> object can be a string now.</p></td></tr>
<tr><td>v0.1.31</td>
<td><p><span>新增于: v0.1.31</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a></p>
<ul>
<li><code>flags</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> 参阅<a href="http://nodejs.cn/s/JjbY8n" rel="nofollow" target="_blank">支持的文件系统标志</a>。<strong>默认值:</strong> <code>'w'</code>。</li>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> <strong>默认值:</strong> <code>'utf8'</code>。</li>
<li><code>fd</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <strong>默认值:</strong> <code>null</code>。</li>
<li><code>mode</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <strong>默认值:</strong> <code>0o666</code>。</li>
<li><code>autoClose</code> <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a> <strong>默认值:</strong> <code>true</code>。</li>
<li><code>start</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
</ul>
</li>
<li>返回: <a href="http://nodejs.cn/s/2uZDVA" target="_blank" rel="nofollow" class="type">&lt;fs.WriteStream&gt;</a> 参阅<a href="http://nodejs.cn/s/9JUnJ8" rel="nofollow" target="_blank">可写流</a>。</li>
</ul>
<p><code>options</code> 可以包括一个 <code>start</code> 选项，允许在文件开头之后的某个位置写入数据。
如果要修改文件而不是覆盖它，则 <code>flags</code> 模式需要为 <code>r+</code> 模式而不是默认的 <code>w</code> 模式。
<code>encoding</code> 可以是 <a href="http://nodejs.cn/s/FApxjh" rel="nofollow" target="_blank"><code>Buffer</code></a> 接受的任何一种字符编码。</p>
<p>如果 <code>autoClose</code> 设置为 <code>true</code>（默认行为），则在 <code>'error'</code> 或 <code>'finish'</code> 事件时文件描述符会自动关闭。
如果 <code>autoClose</code> 为 <code>false</code>，则即使出现错误，也不会关闭文件描述符。
应用程序负责关闭它并确保没有文件描述符泄漏。</p>
<p>与 <a href="http://nodejs.cn/s/C3Eioq" rel="nofollow" target="_blank"><code>ReadStream</code></a> 类似，如果指定了 <code>fd</code>，则 <a href="http://nodejs.cn/s/2uZDVA" rel="nofollow" target="_blank"><code>WriteStream</code></a> 将忽略 <code>path</code> 参数并使用指定的文件描述符。
这意味着不会触发 <code>'open'</code> 事件。
<code>fd</code> 必须是阻塞的，非阻塞的 <code>fd</code> 应该传给 <a href="http://nodejs.cn/s/wsJ1o1" rel="nofollow" target="_blank"><code>net.Socket</code></a>。</p>
<p>如果 <code>options</code> 是字符串，则它指定字符编码。</p>
<h2>fs.exists(path, callback)<span><a class="mark" href="#fs_fs_exists_path_callback" id="fs_fs_exists_path_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_exists_path_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/rfkSgy" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v1.0.0</td>
<td><p><span>废弃于: v1.0.0</span></p></td></tr>
<tr><td>v0.0.2</td>
<td><p><span>新增于: v0.0.2</span></p></td></tr>
</tbody></table>
</details>
</div>
<p></p><div class="api_stability api_stability_0"><a target="_blank" rel="nofollow" href="http://nodejs.cn/api/documentation.html#documentation_stability_index">稳定性: 0</a> - 废弃: 改为使用 <a href="http://nodejs.cn/s/fdrjxj" rel="nofollow" target="_blank"><code>fs.stat()</code></a> 或 <a href="http://nodejs.cn/s/NCPsM3" rel="nofollow" target="_blank"><code>fs.access()</code></a>。</div><p></p>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>exists</code> <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a></li>
</ul>
</li>
</ul>
<p>通过检查文件系统来测试给定的路径是否存在。
然后调用 <code>callback</code> 并带上参数 <code>true</code> 或 <code>false</code>：</p>
<pre class="sh_sourceCode"><code class="language-js">fs<span class="sh_symbol">.</span><span class="sh_function">exists</span><span class="sh_symbol">(</span><span class="sh_string">'/etc/passwd'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>exists<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span>exists <span class="sh_symbol">?</span> <span class="sh_string">'存在'</span> <span class="sh_symbol">:</span> <span class="sh_string">'不存在'</span><span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<p>此回调的参数与其他 Node.js 回调不一致。
通常，Node.js 回调的第一个参数是 <code>err</code> 参数，后面可选地跟随其他参数。
<code>fs.exists()</code> 的回调只有一个布尔值参数。
这是推荐 <code>fs.access()</code> 代替 <code>fs.exists()</code> 的原因之一。</p>
<p>不建议在调用 <code>fs.open()</code>、 <code>fs.readFile()</code> 或 <code>fs.writeFile()</code> 之前使用 <code>fs.exists()</code> 检查文件是否存在。
这样做会引入竞态条件，因为其他进程可能会在两次调用之间更改文件的状态。
相反，应该直接打开、读取或写入文件，如果文件不存在则处理引发的错误。</p>
<p><strong>写入（不推荐）</strong></p>
<pre class="sh_sourceCode"><code class="language-js">fs<span class="sh_symbol">.</span><span class="sh_function">exists</span><span class="sh_symbol">(</span><span class="sh_string">'myfile'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>exists<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>exists<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span>
    console<span class="sh_symbol">.</span><span class="sh_function">error</span><span class="sh_symbol">(</span><span class="sh_string">'myfile 已存在'</span><span class="sh_symbol">);</span>
  <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_cbracket">{</span>
    fs<span class="sh_symbol">.</span><span class="sh_function">open</span><span class="sh_symbol">(</span><span class="sh_string">'myfile'</span><span class="sh_symbol">,</span> <span class="sh_string">'wx'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">,</span> fd<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
      <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
      <span class="sh_function">writeMyData</span><span class="sh_symbol">(</span>fd<span class="sh_symbol">);</span>
    <span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
  <span class="sh_cbracket">}</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<p><strong>写入（推荐）</strong></p>
<pre class="sh_sourceCode"><code class="language-js">fs<span class="sh_symbol">.</span><span class="sh_function">open</span><span class="sh_symbol">(</span><span class="sh_string">'myfile'</span><span class="sh_symbol">,</span> <span class="sh_string">'wx'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">,</span> fd<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span>
    <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">.</span>code <span class="sh_symbol">===</span> <span class="sh_string">'EEXIST'</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span>
      console<span class="sh_symbol">.</span><span class="sh_function">error</span><span class="sh_symbol">(</span><span class="sh_string">'myfile 已存在'</span><span class="sh_symbol">);</span>
      <span class="sh_keyword">return</span><span class="sh_symbol">;</span>
    <span class="sh_cbracket">}</span>

    <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
  <span class="sh_cbracket">}</span>

  <span class="sh_function">writeMyData</span><span class="sh_symbol">(</span>fd<span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<p><strong>读取（不推荐）</strong></p>
<pre class="sh_sourceCode"><code class="language-js">fs<span class="sh_symbol">.</span><span class="sh_function">exists</span><span class="sh_symbol">(</span><span class="sh_string">'myfile'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>exists<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>exists<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span>
    fs<span class="sh_symbol">.</span><span class="sh_function">open</span><span class="sh_symbol">(</span><span class="sh_string">'myfile'</span><span class="sh_symbol">,</span> <span class="sh_string">'r'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">,</span> fd<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
      <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
      <span class="sh_function">readMyData</span><span class="sh_symbol">(</span>fd<span class="sh_symbol">);</span>
    <span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
  <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_cbracket">{</span>
    console<span class="sh_symbol">.</span><span class="sh_function">error</span><span class="sh_symbol">(</span><span class="sh_string">'myfile 不存在'</span><span class="sh_symbol">);</span>
  <span class="sh_cbracket">}</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<p><strong>读取（推荐）</strong></p>
<pre class="sh_sourceCode"><code class="language-js">fs<span class="sh_symbol">.</span><span class="sh_function">open</span><span class="sh_symbol">(</span><span class="sh_string">'myfile'</span><span class="sh_symbol">,</span> <span class="sh_string">'r'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">,</span> fd<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span>
    <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">.</span>code <span class="sh_symbol">===</span> <span class="sh_string">'ENOENT'</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span>
      console<span class="sh_symbol">.</span><span class="sh_function">error</span><span class="sh_symbol">(</span><span class="sh_string">'myfile 不存在'</span><span class="sh_symbol">);</span>
      <span class="sh_keyword">return</span><span class="sh_symbol">;</span>
    <span class="sh_cbracket">}</span>

    <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
  <span class="sh_cbracket">}</span>

  <span class="sh_function">readMyData</span><span class="sh_symbol">(</span>fd<span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<p>上面的“不推荐”示例会先检查文件是否存在然后再使用该文件。
“推荐”示例则更好，因为它们直接使用文件并处理错误（如果有错误的话）。</p>
<p>通常，仅在文件不直接使用时才检查文件是否存在，例如当其存在性是来自另一个进程的信号时。</p>
<h2>fs.existsSync(path)<span><a class="mark" href="#fs_fs_existssync_path" id="fs_fs_existssync_path">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_existssync_path.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/hZHb2a" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v0.1.21</td>
<td><p><span>新增于: v0.1.21</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>返回: <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a></li>
</ul>
<p>如果路径存在，则返回 <code>true</code>，否则返回 <code>false</code>。</p>
<p>有关详细信息，参阅此 API 的异步版本的文档：<a href="http://nodejs.cn/s/udxWhD" rel="nofollow" target="_blank"><code>fs.exists()</code></a>。</p>
<p>虽然 <code>fs.exists()</code> 已废弃，但 <code>fs.existsSync()</code> 不是废弃的。
<code>fs.exists()</code> 的 <code>callback</code> 参数接受与其他 Node.js 回调不一致的参数。
<code>fs.existsSync()</code> 不使用回调。</p>
<h2>fs.fchmod(fd, mode, callback)<span><a class="mark" href="#fs_fs_fchmod_fd_mode_callback" id="fs_fs_fchmod_fd_mode_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_fchmod_fd_mode_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/mVN6PX" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.4.7</td>
<td><p><span>新增于: v0.4.7</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>mode</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
</ul>
</li>
</ul>
<p>异步的 <a href="http://nodejs.cn/s/L3LQRh" rel="nofollow" target="_blank"><code>fchmod(2)</code></a>。
除了可能的异常，完成回调没有其他参数。</p>
<h2>fs.fchmodSync(fd, mode)<span><a class="mark" href="#fs_fs_fchmodsync_fd_mode" id="fs_fs_fchmodsync_fd_mode">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_fchmodsync_fd_mode.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/axLzon" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.4.7</span>
</div>
<ul>
<li><code>fd</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>mode</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
</ul>
<p>同步的 <a href="http://nodejs.cn/s/L3LQRh" rel="nofollow" target="_blank"><code>fchmod(2)</code></a>。返回 <code>undefined</code>。</p>
<h2>fs.fchown(fd, uid, gid, callback)<span><a class="mark" href="#fs_fs_fchown_fd_uid_gid_callback" id="fs_fs_fchown_fd_uid_gid_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_fchown_fd_uid_gid_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/e4jFPN" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.4.7</td>
<td><p><span>新增于: v0.4.7</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>uid</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>gid</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
</ul>
</li>
</ul>
<p>异步的 <a href="http://nodejs.cn/s/vZfXQ2" rel="nofollow" target="_blank"><code>fchown(2)</code></a>。
除了可能的异常，完成回调没有其他参数。</p>
<h2>fs.fchownSync(fd, uid, gid)<span><a class="mark" href="#fs_fs_fchownsync_fd_uid_gid" id="fs_fs_fchownsync_fd_uid_gid">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_fchownsync_fd_uid_gid.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/sP2Jzd" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.4.7</span>
</div>
<ul>
<li><code>fd</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>uid</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>gid</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
</ul>
<p>同步的 <a href="http://nodejs.cn/s/vZfXQ2" rel="nofollow" target="_blank"><code>fchown(2)</code></a>。返回 <code>undefined</code>。</p>
<h2>fs.fdatasync(fd, callback)<span><a class="mark" href="#fs_fs_fdatasync_fd_callback" id="fs_fs_fdatasync_fd_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_fdatasync_fd_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/qU6jdW" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.1.96</td>
<td><p><span>新增于: v0.1.96</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
</ul>
</li>
</ul>
<p>异步的 <a href="http://nodejs.cn/s/AEp6fv" rel="nofollow" target="_blank"><code>fdatasync(2)</code></a>。
除了可能的异常，完成回调没有其他参数。</p>
<h2>fs.fdatasyncSync(fd)<span><a class="mark" href="#fs_fs_fdatasyncsync_fd" id="fs_fs_fdatasyncsync_fd">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_fdatasyncsync_fd.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/qKzTzX" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.1.96</span>
</div>
<ul>
<li><code>fd</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
</ul>
<p>同步的 <a href="http://nodejs.cn/s/AEp6fv" rel="nofollow" target="_blank"><code>fdatasync(2)</code></a>。返回 <code>undefined</code>。</p>
<h2>fs.fstat(fd[, options], callback)<span><a class="mark" href="#fs_fs_fstat_fd_options_callback" id="fs_fs_fstat_fd_options_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_fstat_fd_options_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/ud64EY" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.5.0</td>
<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.1.95</td>
<td><p><span>新增于: v0.1.95</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a></p>
<ul>
<li><code>bigint</code> <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a> 返回的 <a href="http://nodejs.cn/s/NMuvVx" rel="nofollow" target="_blank"><code>fs.Stats</code></a> 对象中的数值是否应为 <code>bigint</code> 型。<strong>默认值:</strong> <code>false</code>。</li>
</ul>
</li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
<li><code>stats</code> <a href="http://nodejs.cn/s/NMuvVx" target="_blank" rel="nofollow" class="type">&lt;fs.Stats&gt;</a></li>
</ul>
</li>
</ul>
<p>异步的 <a href="http://nodejs.cn/s/anrsLJ" rel="nofollow" target="_blank"><code>fstat(2)</code></a>。
回调会传入两个参数 <code>(err, stats)</code>，其中 <code>stats</code> 是 <a href="http://nodejs.cn/s/NMuvVx" rel="nofollow" target="_blank"><code>fs.Stats</code></a> 对象。
<code>fstat()</code> 与 <a href="http://nodejs.cn/s/fdrjxj" rel="nofollow" target="_blank"><code>stat()</code></a> 相同，除了文件是由文件描述符 <code>fd</code> 指定。</p>
<h2>fs.fstatSync(fd[, options])<span><a class="mark" href="#fs_fs_fstatsync_fd_options" id="fs_fs_fstatsync_fd_options">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_fstatsync_fd_options.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/upst4E" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.5.0</td>
<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr>
<tr><td>v0.1.95</td>
<td><p><span>新增于: v0.1.95</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a></p>
<ul>
<li><code>bigint</code> <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a> 返回的 <a href="http://nodejs.cn/s/NMuvVx" rel="nofollow" target="_blank"><code>fs.Stats</code></a> 对象中的数值是否应为 <code>bigint</code> 型。<strong>默认值:</strong> <code>false</code>。</li>
</ul>
</li>
<li>返回: <a href="http://nodejs.cn/s/NMuvVx" target="_blank" rel="nofollow" class="type">&lt;fs.Stats&gt;</a></li>
</ul>
<p>同步的 <a href="http://nodejs.cn/s/anrsLJ" rel="nofollow" target="_blank"><code>fstat(2)</code></a>。</p>
<h2>fs.fsync(fd, callback)<span><a class="mark" href="#fs_fs_fsync_fd_callback" id="fs_fs_fsync_fd_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_fsync_fd_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/mjcfdL" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.1.96</td>
<td><p><span>新增于: v0.1.96</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
</ul>
</li>
</ul>
<p>异步的 <a href="http://nodejs.cn/s/d4u3Ks" rel="nofollow" target="_blank"><code>fsync(2)</code></a>。
除了可能的异常，完成回调没有其他参数。</p>
<h2>fs.fsyncSync(fd)<span><a class="mark" href="#fs_fs_fsyncsync_fd" id="fs_fs_fsyncsync_fd">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_fsyncsync_fd.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/2zKi5Z" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.1.96</span>
</div>
<ul>
<li><code>fd</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
</ul>
<p>同步的 <a href="http://nodejs.cn/s/d4u3Ks" rel="nofollow" target="_blank"><code>fsync(2)</code></a>。返回 <code>undefined</code>。</p>
<h2>fs.ftruncate(fd[, len], callback)<span><a class="mark" href="#fs_fs_ftruncate_fd_len_callback" id="fs_fs_ftruncate_fd_len_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_ftruncate_fd_len_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/yiMg3B" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.8.6</td>
<td><p><span>新增于: v0.8.6</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>len</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <strong>默认值:</strong> <code>0</code>。</li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
</ul>
</li>
</ul>
<p>异步的 <a href="http://nodejs.cn/s/nU6hxN" rel="nofollow" target="_blank"><code>ftruncate(2)</code></a>。
除了可能的异常，完成回调没有其他参数。</p>
<p>如果文件描述符指向的文件大于 <code>len</code> 个字节，则只有前面 <code>len</code> 个字节会保留在文件中。</p>
<p>例如，以下程序只保留文件的前 4 个字节：</p>
<pre class="sh_sourceCode"><code class="language-js">console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span>fs<span class="sh_symbol">.</span><span class="sh_function">readFileSync</span><span class="sh_symbol">(</span><span class="sh_string">'temp.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'utf8'</span><span class="sh_symbol">));</span>
<span class="sh_comment">// 输出: Node.js</span>

<span class="sh_comment">// 获取要截断的文件的文件描述符。</span>
<span class="sh_keyword">const</span> fd <span class="sh_symbol">=</span> fs<span class="sh_symbol">.</span><span class="sh_function">openSync</span><span class="sh_symbol">(</span><span class="sh_string">'temp.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'r+'</span><span class="sh_symbol">);</span>

<span class="sh_comment">// 将文件截断为前 4 个字节。</span>
fs<span class="sh_symbol">.</span><span class="sh_function">ftruncate</span><span class="sh_symbol">(</span>fd<span class="sh_symbol">,</span> <span class="sh_number">4</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  assert<span class="sh_symbol">.</span><span class="sh_function">ifError</span><span class="sh_symbol">(</span>err<span class="sh_symbol">);</span>
  console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span>fs<span class="sh_symbol">.</span><span class="sh_function">readFileSync</span><span class="sh_symbol">(</span><span class="sh_string">'temp.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'utf8'</span><span class="sh_symbol">));</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
<span class="sh_comment">// 输出: Node</span>
</code></pre>
<p>如果文件小于 <code>len</code> 个字节，则会对其进行扩展，并且扩展部分将填充空字节（<code>'\0'</code>）：</p>
<pre class="sh_sourceCode"><code class="language-js">console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span>fs<span class="sh_symbol">.</span><span class="sh_function">readFileSync</span><span class="sh_symbol">(</span><span class="sh_string">'temp.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'utf8'</span><span class="sh_symbol">));</span>
<span class="sh_comment">// 输出: Node.js</span>

<span class="sh_comment">// 获取要截断的文件的文件描述符。</span>
<span class="sh_keyword">const</span> fd <span class="sh_symbol">=</span> fs<span class="sh_symbol">.</span><span class="sh_function">openSync</span><span class="sh_symbol">(</span><span class="sh_string">'temp.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'r+'</span><span class="sh_symbol">);</span>

<span class="sh_comment">// 将文件截断为前 10 个字节，但实际大小为 7 个字节。</span>
fs<span class="sh_symbol">.</span><span class="sh_function">ftruncate</span><span class="sh_symbol">(</span>fd<span class="sh_symbol">,</span> <span class="sh_number">10</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  assert<span class="sh_symbol">.</span><span class="sh_function">ifError</span><span class="sh_symbol">(</span>err<span class="sh_symbol">);</span>
  console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span>fs<span class="sh_symbol">.</span><span class="sh_function">readFileSync</span><span class="sh_symbol">(</span><span class="sh_string">'temp.txt'</span><span class="sh_symbol">));</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
<span class="sh_comment">// 输出: &lt;Buffer 4e 6f 64 65 2e 6a 73 00 00 00&gt;</span>
<span class="sh_comment">// (UTF8 的值为 'Node.js\0\0\0')</span>
</code></pre>
<p>最后 3 个字节是空字节（<code>'\0'</code>），以补充超出的截断。</p>
<h2>fs.ftruncateSync(fd[, len])<span><a class="mark" href="#fs_fs_ftruncatesync_fd_len" id="fs_fs_ftruncatesync_fd_len">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_ftruncatesync_fd_len.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/PJVLUf" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.8.6</span>
</div>
<ul>
<li><code>fd</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>len</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <strong>默认值:</strong> <code>0</code>。</li>
</ul>
<p>返回 <code>undefined</code>。</p>
<p>有关详细信息，请参阅此API的异步版本的文档：<a href="http://nodejs.cn/s/dRb1jt" rel="nofollow" target="_blank"><code>fs.ftruncate()</code></a>。</p>
<h2>fs.futimes(fd, atime, mtime, callback)<span><a class="mark" href="#fs_fs_futimes_fd_atime_mtime_callback" id="fs_fs_futimes_fd_atime_mtime_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_futimes_fd_atime_mtime_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/L7czUQ" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v4.1.0</td>
<td><p>Numeric strings, <code>NaN</code> and <code>Infinity</code> are now allowed time specifiers.</p></td></tr>
<tr><td>v0.4.2</td>
<td><p><span>新增于: v0.4.2</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>atime</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> | <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/A9TMoa" target="_blank" rel="nofollow" class="type">&lt;Date&gt;</a></li>
<li><code>mtime</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> | <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/A9TMoa" target="_blank" rel="nofollow" class="type">&lt;Date&gt;</a></li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
</ul>
</li>
</ul>
<p>更改文件描述符指向的对象的文件系统时间戳。
参阅 <a href="http://nodejs.cn/s/UxAPRV" rel="nofollow" target="_blank"><code>fs.utimes()</code></a>。</p>
<p>此函数在 7.1 之前的 AIX 版本上不起作用，它将返回 <code>UV_ENOSYS</code> 错误。</p>
<h2>fs.futimesSync(fd, atime, mtime)<span><a class="mark" href="#fs_fs_futimessync_fd_atime_mtime" id="fs_fs_futimessync_fd_atime_mtime">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_futimessync_fd_atime_mtime.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/y98A73" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v4.1.0</td>
<td><p>Numeric strings, <code>NaN</code> and <code>Infinity</code> are now allowed time specifiers.</p></td></tr>
<tr><td>v0.4.2</td>
<td><p><span>新增于: v0.4.2</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>atime</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>mtime</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
</ul>
<p><a href="http://nodejs.cn/s/Pxb3N1" rel="nofollow" target="_blank"><code>fs.futimes()</code></a> 的同步版本。返回 <code>undefined</code>。</p>
<h2>fs.lchmod(path, mode, callback)<span><a class="mark" href="#fs_fs_lchmod_path_mode_callback" id="fs_fs_lchmod_path_mode_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_lchmod_path_mode_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/KM7cuT" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.4.7</td>
<td><p><span>废弃于: v0.4.7</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>mode</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
</ul>
</li>
</ul>
<p>异步的 <a href="http://nodejs.cn/s/yC9BxV" rel="nofollow" target="_blank"><code>lchmod(2)</code></a>。
除了可能的异常，完成回调没有其他参数。</p>
<p>仅适用于 macOS。</p>
<h2>fs.lchmodSync(path, mode)<span><a class="mark" href="#fs_fs_lchmodsync_path_mode" id="fs_fs_lchmodsync_path_mode">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_lchmodsync_path_mode.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/gMhj1W" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>废弃于: v0.4.7</span>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>mode</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
</ul>
<p>同步的 <a href="http://nodejs.cn/s/yC9BxV" rel="nofollow" target="_blank"><code>lchmod(2)</code></a>。返回 <code>undefined</code>。</p>
<h2>fs.lchown(path, uid, gid, callback)<span><a class="mark" href="#fs_fs_lchown_path_uid_gid_callback" id="fs_fs_lchown_path_uid_gid_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_lchown_path_uid_gid_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/RdzSsJ" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.6.0</td>
<td><p>This API is no longer deprecated.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>uid</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>gid</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
</ul>
</li>
</ul>
<p>异步的 <a href="http://nodejs.cn/s/fGGBXD" rel="nofollow" target="_blank"><code>lchown(2)</code></a>。
除了可能的异常，完成回调没有其他参数。</p>
<h2>fs.lchownSync(path, uid, gid)<span><a class="mark" href="#fs_fs_lchownsync_path_uid_gid" id="fs_fs_lchownsync_path_uid_gid">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_lchownsync_path_uid_gid.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/wZawMa" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.6.0</td>
<td><p>This API is no longer deprecated.</p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>uid</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>gid</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
</ul>
<p>同步的 <a href="http://nodejs.cn/s/fGGBXD" rel="nofollow" target="_blank"><code>lchown(2)</code></a>。返回 <code>undefined</code>。</p>
<h2>fs.link(existingPath, newPath, callback)<span><a class="mark" href="#fs_fs_link_existingpath_newpath_callback" id="fs_fs_link_existingpath_newpath_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_link_existingpath_newpath_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/rZ8gvT" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>existingPath</code> and <code>newPath</code> parameters can be WHATWG <code>URL</code> objects using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.1.31</td>
<td><p><span>新增于: v0.1.31</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>existingPath</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>newPath</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
</ul>
</li>
</ul>
<p>异步的 <a href="http://nodejs.cn/s/U8H1mr" rel="nofollow" target="_blank"><code>link(2)</code></a>。
除了可能的异常，完成回调没有其他参数。</p>
<h2>fs.linkSync(existingPath, newPath)<span><a class="mark" href="#fs_fs_linksync_existingpath_newpath" id="fs_fs_linksync_existingpath_newpath">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_linksync_existingpath_newpath.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/Jv3GiT" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>existingPath</code> and <code>newPath</code> parameters can be WHATWG <code>URL</code> objects using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v0.1.31</td>
<td><p><span>新增于: v0.1.31</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>existingPath</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>newPath</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
</ul>
<p>同步的 <a href="http://nodejs.cn/s/U8H1mr" rel="nofollow" target="_blank"><code>link(2)</code></a>。返回 <code>undefined</code>。</p>
<h2>fs.lstat(path[, options], callback)<span><a class="mark" href="#fs_fs_lstat_path_options_callback" id="fs_fs_lstat_path_options_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_lstat_path_options_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/tJxsnS" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.5.0</td>
<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.1.30</td>
<td><p><span>新增于: v0.1.30</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a></p>
<ul>
<li><code>bigint</code> <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a> 返回的 <a href="http://nodejs.cn/s/NMuvVx" rel="nofollow" target="_blank"><code>fs.Stats</code></a> 对象中的数值是否应为 <code>bigint</code> 型。<strong>默认值:</strong> <code>false</code>。</li>
</ul>
</li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
<li><code>stats</code> <a href="http://nodejs.cn/s/NMuvVx" target="_blank" rel="nofollow" class="type">&lt;fs.Stats&gt;</a></li>
</ul>
</li>
</ul>
<p>异步的 <a href="http://nodejs.cn/s/fGaHTu" rel="nofollow" target="_blank"><code>lstat(2)</code></a>。
回调会传入两个参数 <code>(err, stats)</code>，其中 <code>stats</code> 是 <a href="http://nodejs.cn/s/NMuvVx" rel="nofollow" target="_blank"><code>fs.Stats</code></a> 对象。
<code>lstat()</code> 与 <a href="http://nodejs.cn/s/fdrjxj" rel="nofollow" target="_blank"><code>stat()</code></a> 相同，只是如果 <code>path</code> 是符号链接，则查看的是链接自身，而不是它指向的文件。</p>
<h2>fs.lstatSync(path[, options])<span><a class="mark" href="#fs_fs_lstatsync_path_options" id="fs_fs_lstatsync_path_options">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_lstatsync_path_options.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/pkXFyg" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.5.0</td>
<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v0.1.30</td>
<td><p><span>新增于: v0.1.30</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a></p>
<ul>
<li><code>bigint</code> <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a> 返回的 <a href="http://nodejs.cn/s/NMuvVx" rel="nofollow" target="_blank"><code>fs.Stats</code></a> 对象中的数值是否应为 <code>bigint</code> 型。<strong>默认值:</strong> <code>false</code>。</li>
</ul>
</li>
<li>返回: <a href="http://nodejs.cn/s/NMuvVx" target="_blank" rel="nofollow" class="type">&lt;fs.Stats&gt;</a></li>
</ul>
<p>同步的 <a href="http://nodejs.cn/s/fGaHTu" rel="nofollow" target="_blank"><code>lstat(2)</code></a>。</p>
<h2>fs.mkdir(path[, options], callback)<span><a class="mark" href="#fs_fs_mkdir_path_options_callback" id="fs_fs_mkdir_path_options_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_mkdir_path_options_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/SzR1cB" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.12.0</td>
<td><p>The second argument can now be an <code>options</code> object with <code>recursive</code> and <code>mode</code> properties.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.1.8</td>
<td><p><span>新增于: v0.1.8</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a> | <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></p>
<ul>
<li><code>recursive</code> <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a> <strong>默认值:</strong> <code>false</code>。</li>
<li><code>mode</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> Windows 上不支持。<strong>默认值:</strong> <code>0o777</code>。</li>
</ul>
</li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
</ul>
</li>
</ul>
<p>异步地创建目录。
除了可能的异常，完成回调没有其他参数。</p>
<p>可选的 <code>options</code> 参数可以是指定模式（权限和粘滞位）的整数，也可以是具有 <code>mode</code> 属性和 <code>recursive</code> 属性（指示是否应创建父文件夹）的对象。</p>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_comment">// 创建 /tmp/a/apple 目录，无论是否存在 /tmp 和 /tmp/a 目录。</span>
fs<span class="sh_symbol">.</span><span class="sh_function">mkdir</span><span class="sh_symbol">(</span><span class="sh_string">'/tmp/a/apple'</span><span class="sh_symbol">,</span> <span class="sh_cbracket">{</span> recursive<span class="sh_symbol">:</span> <span class="sh_keyword">true</span> <span class="sh_cbracket">}</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<p>也可参阅 <a href="http://nodejs.cn/s/ebLzdi" rel="nofollow" target="_blank"><code>mkdir(2)</code></a>。</p>
<h2>fs.mkdirSync(path[, options])<span><a class="mark" href="#fs_fs_mkdirsync_path_options" id="fs_fs_mkdirsync_path_options">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_mkdirsync_path_options.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/2KmeZW" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.12.0</td>
<td><p>The second argument can now be an <code>options</code> object with <code>recursive</code> and <code>mode</code> properties.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v0.1.21</td>
<td><p><span>新增于: v0.1.21</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a> | <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></p>
<ul>
<li><code>recursive</code> <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a> <strong>默认值:</strong> <code>false</code>。</li>
<li><code>mode</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> Windows 上不支持。<strong>默认值:</strong> <code>0o777</code>。</li>
</ul>
</li>
</ul>
<p>同步地创建目录。
返回 <code>undefined</code>。
<a href="http://nodejs.cn/s/Adhmvz" rel="nofollow" target="_blank"><code>fs.mkdir()</code></a> 的同步版本。</p>
<p>也可参阅 <a href="http://nodejs.cn/s/ebLzdi" rel="nofollow" target="_blank"><code>mkdir(2)</code></a>。</p>
<h2>fs.mkdtemp(prefix[, options], callback)<span><a class="mark" href="#fs_fs_mkdtemp_prefix_options_callback" id="fs_fs_mkdtemp_prefix_options_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_mkdtemp_prefix_options_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/NYsnm7" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v6.2.1</td>
<td><p>The <code>callback</code> parameter is optional now.</p></td></tr>
<tr><td>v5.10.0</td>
<td><p><span>新增于: v5.10.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>prefix</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a></p>
<ul>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> <strong>默认值:</strong> <code>'utf8'</code>。</li>
</ul>
</li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
<li><code>folder</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a></li>
</ul>
</li>
</ul>
<p>创建一个唯一的临时目录。</p>
<p>生成要附加在必需的 <code>prefix</code> 后面的六位随机字符，以创建唯一的临时目录。</p>
<p>创建的目录路径作为字符串传给回调的第二个参数。</p>
<p>可选的 <code>options</code> 参数可以是指定字符编码的字符串，也可以是具有指定要使用的字符编码的 <code>encoding</code> 属性的对象。</p>
<pre class="sh_sourceCode"><code class="language-js">fs<span class="sh_symbol">.</span><span class="sh_function">mkdtemp</span><span class="sh_symbol">(</span>path<span class="sh_symbol">.</span><span class="sh_function">join</span><span class="sh_symbol">(</span>os<span class="sh_symbol">.</span><span class="sh_function">tmpdir</span><span class="sh_symbol">(),</span> <span class="sh_string">'目录-'</span><span class="sh_symbol">),</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">,</span> folder<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
  console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span>folder<span class="sh_symbol">);</span>
  <span class="sh_comment">// 输出: /tmp/目录-itXde2 或 C:\Users\...\AppData\Local\Temp\目录-itXde2</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<p><code>fs.mkdtemp()</code> 方法将六位随机选择的字符直接附加到 <code>prefix</code> 字符串。
例如，给定目录 <code>/tmp</code>，如果打算在 <code>/tmp</code> 中创建临时目录，则 <code>prefix</code> 必须在尾部加上特定平台的路径分隔符（<code>require('path').sep</code>）。</p>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_comment">// 新的临时目录的父目录。</span>
<span class="sh_keyword">const</span> tmpDir <span class="sh_symbol">=</span> os<span class="sh_symbol">.</span><span class="sh_function">tmpdir</span><span class="sh_symbol">();</span>

<span class="sh_comment">// 此用法是错误的：</span>
fs<span class="sh_symbol">.</span><span class="sh_function">mkdtemp</span><span class="sh_symbol">(</span>tmpDir<span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">,</span> folder<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
  console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span>folder<span class="sh_symbol">);</span>
  <span class="sh_comment">// 输出类似 `/tmpabc123`。</span>
  <span class="sh_comment">// 新的临时目录会被创建在文件系统根目录，而不是在 /tmp 目录中。</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>

<span class="sh_comment">// 此用法是正确的：</span>
<span class="sh_keyword">const</span> <span class="sh_cbracket">{</span> sep <span class="sh_cbracket">}</span> <span class="sh_symbol">=</span> <span class="sh_function">require</span><span class="sh_symbol">(</span><span class="sh_string">'path'</span><span class="sh_symbol">);</span>
fs<span class="sh_symbol">.</span><span class="sh_function">mkdtemp</span><span class="sh_symbol">(</span>`$<span class="sh_cbracket">{</span>tmpDir<span class="sh_cbracket">}</span>$<span class="sh_cbracket">{</span>sep<span class="sh_cbracket">}</span>`<span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">,</span> folder<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
  console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span>folder<span class="sh_symbol">);</span>
  <span class="sh_comment">// 输出类似 `/tmp/abc123`。</span>
  <span class="sh_comment">// 新的临时目录会被创建在 /tmp 目录中。</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<h2>fs.mkdtempSync(prefix[, options])<span><a class="mark" href="#fs_fs_mkdtempsync_prefix_options" id="fs_fs_mkdtempsync_prefix_options">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_mkdtempsync_prefix_options.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/zs3BPV" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v5.10.0</span>
</div>
<ul>
<li><code>prefix</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a></p>
<ul>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> <strong>默认值:</strong> <code>'utf8'</code>。</li>
</ul>
</li>
<li>返回: <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a></li>
</ul>
<p>返回创建的目录路径。</p>
<p>有关详细信息，请参阅此 API 异步版本的文档：<a href="http://nodejs.cn/s/gPmez5" rel="nofollow" target="_blank"><code>fs.mkdtemp()</code></a>。</p>
<p>可选的 <code>options</code> 参数可以是指定字符编码的字符串，也可以是具有指定要使用的字符编码的 <code>encoding</code> 属性的对象。</p>
<h2>fs.open(path, flags[, mode], callback)<span><a class="mark" href="#fs_fs_open_path_flags_mode_callback" id="fs_fs_open_path_flags_mode_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_open_path_flags_mode_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/cwP65n" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v11.1.0</td>
<td><p>The <code>flags</code> argument is now optional and defaults to <code>'r'</code>.</p></td></tr>
<tr><td>v9.9.0</td>
<td><p>The <code>as</code> and <code>as+</code> modes are supported now.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v0.0.2</td>
<td><p><span>新增于: v0.0.2</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>flags</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> 参阅<a href="http://nodejs.cn/s/JjbY8n" rel="nofollow" target="_blank">支持的文件系统标志</a>。</li>
<li><code>mode</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <strong>默认值:</strong> <code>0o666</code>（可读写）。</li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
<li><code>fd</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
</ul>
</li>
</ul>
<p>异步地打开文件。参阅 <a href="http://nodejs.cn/s/ss2dGs" rel="nofollow" target="_blank"><code>open(2)</code></a>。</p>
<p><code>mode</code> 设置文件模式（权限和粘滞位），但仅限于创建文件的情况。
在 Windows 上，只能操作写权限，参阅 <a href="http://nodejs.cn/s/6QUJs8" rel="nofollow" target="_blank"><code>fs.chmod()</code></a>。</p>
<p>回调有两个参数 <code>(err, fd)</code>。</p>
<p>有些字符 (<code>&lt; &gt; : " / \ | ? *</code>) 在 Windows 上是预留的，参阅<a href="http://nodejs.cn/s/GmL95W" rel="nofollow" target="_blank">命名文件、路径以及命名空间</a>。
在 NTFS 上，如果文件名包含冒号，则 Node.js 将打开文件系统流，参阅<a href="http://nodejs.cn/s/FY8iKV" rel="nofollow" target="_blank">此 MSDN 页面</a>。</p>
<p>基于 <code>fs.open()</code> 的函数也表现出以上行为，比如 <code>fs.writeFile()</code>、 <code>fs.readFile()</code> 等。</p>
<h2>fs.openSync(path, flags[, mode])<span><a class="mark" href="#fs_fs_opensync_path_flags_mode" id="fs_fs_opensync_path_flags_mode">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_opensync_path_flags_mode.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/UDMLLP" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v11.1.0</td>
<td><p>The <code>flags</code> argument is now optional and defaults to <code>'r'</code>.</p></td></tr>
<tr><td>v9.9.0</td>
<td><p>The <code>as</code> and <code>as+</code> modes are supported now.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v0.1.21</td>
<td><p><span>新增于: v0.1.21</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>flags</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> 参阅<a href="http://nodejs.cn/s/JjbY8n" rel="nofollow" target="_blank">支持的文件系统标志</a>。</li>
<li><code>mode</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <strong>默认值:</strong> <code>0o666</code>。</li>
<li>返回: <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a></li>
</ul>
<p>返回表示文件描述符的整数。</p>
<p>有关详细信息，参阅此 API 的异步版本的文档：<a href="http://nodejs.cn/s/9Q96Bd" rel="nofollow" target="_blank"><code>fs.open()</code></a>。</p>
<h2>fs.read(fd, buffer, offset, length, position, callback)<span><a class="mark" href="#fs_fs_read_fd_buffer_offset_length_position_callback" id="fs_fs_read_fd_buffer_offset_length_position_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_read_fd_buffer_offset_length_position_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/QteCXH" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.10.0</td>
<td><p>The <code>buffer</code> parameter can now be any <code>TypedArray</code>, or a <code>DataView</code>.</p></td></tr>
<tr><td>v7.4.0</td>
<td><p>The <code>buffer</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr>
<tr><td>v6.0.0</td>
<td><p>The <code>length</code> parameter can now be <code>0</code>.</p></td></tr>
<tr><td>v0.0.2</td>
<td><p><span>新增于: v0.0.2</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>buffer</code> <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/oh3CkV" target="_blank" rel="nofollow" class="type">&lt;TypedArray&gt;</a> | <a href="http://nodejs.cn/s/yCdVkD" target="_blank" rel="nofollow" class="type">&lt;DataView&gt;</a></li>
<li><code>offset</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>length</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>position</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
<li><code>bytesRead</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>buffer</code> <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a></li>
</ul>
</li>
</ul>
<p>从 <code>fd</code> 指定的文件中读取数据。</p>
<p><code>buffer</code> 是数据将写入的缓冲区。</p>
<p><code>offset</code> 是 buffer 中开始写入的偏移量。</p>
<p><code>length</code> 是一个整数，指定要读取的字节数。</p>
<p><code>position</code> 参数指定从文件中开始读取的位置。
如果 <code>position</code> 为 <code>null</code>，则从当前文件位置读取数据，并更新文件位置。
如果 <code>position</code> 是整数，则文件位置将保持不变。</p>
<p>回调有三个参数 <code>(err, bytesRead, buffer)</code>。</p>
<p>如果调用此方法的 <a href="http://nodejs.cn/s/DGMNHh" rel="nofollow" target="_blank"><code>util.promisify()</code></a> 版本，则返回的 <code>Promise</code> 会返回具有 <code>bytesRead</code> 属性和 <code>buffer</code> 属性的 <code>Object</code>。</p>
<h2>fs.readdir(path[, options], callback)<span><a class="mark" href="#fs_fs_readdir_path_options_callback" id="fs_fs_readdir_path_options_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_readdir_path_options_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/umBCr8" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.10.0</td>
<td><p>New option <code>withFileTypes</code> was added.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v6.0.0</td>
<td><p>The <code>options</code> parameter was added.</p></td></tr>
<tr><td>v0.1.8</td>
<td><p><span>新增于: v0.1.8</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a></p>
<ul>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> <strong>默认值:</strong> <code>'utf8'</code>。</li>
<li><code>withFileTypes</code> <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a> <strong>默认值:</strong> <code>false</code>。</li>
</ul>
</li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
<li><code>files</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string[]&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer[]&gt;</a> | <a href="http://nodejs.cn/s/mNLVJ7" target="_blank" rel="nofollow" class="type">&lt;fs.Dirent[]&gt;</a></li>
</ul>
</li>
</ul>
<p>异步的 <a href="http://nodejs.cn/s/QvrbKw" rel="nofollow" target="_blank"><code>readdir(3)</code></a>。
读取目录的内容。
回调有两个参数 <code>(err, files)</code>，其中 <code>files</code> 是目录中的文件名的数组（不包括 <code>'.'</code> 和 <code>'..'</code>）。</p>
<p>可选的 <code>options</code> 参数可以是指定编码的字符串，也可以是具有 <code>encoding</code> 属性的对象，该属性指定用于传给回调的文件名的字符编码。
如果 <code>encoding</code> 设置为 <code>'buffer'</code>，则返回的文件名是 <code>Buffer</code> 对象。</p>
<p>如果 <code>options.withFileTypes</code> 设置为 <code>true</code>，则 <code>files</code> 数组将包含 <a href="http://nodejs.cn/s/mNLVJ7" rel="nofollow" target="_blank"><code>fs.Dirent</code></a> 对象。</p>
<h2>fs.readdirSync(path[, options])<span><a class="mark" href="#fs_fs_readdirsync_path_options" id="fs_fs_readdirsync_path_options">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_readdirsync_path_options.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/oW1dZS" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.10.0</td>
<td><p>New option <code>withFileTypes</code> was added.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v0.1.21</td>
<td><p><span>新增于: v0.1.21</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a></p>
<ul>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> <strong>默认值:</strong> <code>'utf8'</code>。</li>
<li><code>withFileTypes</code> <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a> <strong>默认值:</strong> <code>false</code>。</li>
</ul>
</li>
<li>返回: <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string[]&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer[]&gt;</a> | <a href="http://nodejs.cn/s/mNLVJ7" target="_blank" rel="nofollow" class="type">&lt;fs.Dirent[]&gt;</a></li>
</ul>
<p>同步的 <a href="http://nodejs.cn/s/QvrbKw" rel="nofollow" target="_blank"><code>readdir(3)</code></a>。</p>
<p>可选的 <code>options</code> 参数可以是指定编码的字符串，也可以是具有 <code>encoding</code> 属性的对象，该属性指定用于传给回调的文件名的字符编码。
如果 <code>encoding</code> 设置为 <code>'buffer'</code>，则返回的文件名是 <code>Buffer</code> 对象。</p>
<p>如果 <code>options.withFileTypes</code> 设置为 <code>true</code>，则返回的结果将包含 <a href="http://nodejs.cn/s/mNLVJ7" rel="nofollow" target="_blank"><code>fs.Dirent</code></a> 对象。</p>
<h2>fs.readFile(path[, options], callback)<span><a class="mark" href="#fs_fs_readfile_path_options_callback" id="fs_fs_readfile_path_options_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_readfile_path_options_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/hRiM3m" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v5.1.0</td>
<td><p>The <code>callback</code> will always be called with <code>null</code> as the <code>error</code> parameter in case of success.</p></td></tr>
<tr><td>v5.0.0</td>
<td><p>The <code>path</code> parameter can be a file descriptor now.</p></td></tr>
<tr><td>v0.1.29</td>
<td><p><span>新增于: v0.1.29</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a> | <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> 文件名或文件描述符。</li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a> | <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a></p>
<ul>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/334hvC" target="_blank" rel="nofollow" class="type">&lt;null&gt;</a> <strong>默认值:</strong> <code>null</code>。</li>
<li><code>flag</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> 参阅<a href="http://nodejs.cn/s/JjbY8n" rel="nofollow" target="_blank">支持的文件系统标志</a>。<strong>默认值:</strong> <code>'r'</code>。</li>
</ul>
</li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
<li><code>data</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a></li>
</ul>
</li>
</ul>
<p>异步地读取文件的全部内容。</p>
<pre class="sh_sourceCode"><code class="language-js">fs<span class="sh_symbol">.</span><span class="sh_function">readFile</span><span class="sh_symbol">(</span><span class="sh_string">'/etc/passwd'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">,</span> data<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
  console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span>data<span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<p>回调会传入两个参数 <code>(err, data)</code>，其中 <code>data</code> 是文件的内容。</p>
<p>如果没有指定 <code>encoding</code>，则返回原始的 buffer。</p>
<p>如果 <code>options</code> 是字符串，则它指定字符编码：</p>
<pre class="sh_sourceCode"><code class="language-js">fs<span class="sh_symbol">.</span><span class="sh_function">readFile</span><span class="sh_symbol">(</span><span class="sh_string">'/etc/passwd'</span><span class="sh_symbol">,</span> <span class="sh_string">'utf8'</span><span class="sh_symbol">,</span> callback<span class="sh_symbol">);</span>
</code></pre>
<p>当 <code>path</code> 是目录时， <code>fs.readFile()</code> 与 <a href="http://nodejs.cn/s/LxKo1q" rel="nofollow" target="_blank"><code>fs.readFileSync()</code></a> 的行为是特定于平台的。
在 macOS、Linux 和 Windows 上，将返回错误。
在 FreeBSD 上，将返回目录内容的表示。</p>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_comment">// 在 macOS、Linux 和 Windows 上：</span>
fs<span class="sh_symbol">.</span><span class="sh_function">readFile</span><span class="sh_symbol">(</span><span class="sh_string">'&lt;目录&gt;'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">,</span> data<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_comment">// =&gt; [Error: EISDIR: illegal operation on a directory, read &lt;目录&gt;]</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>

<span class="sh_comment">// 在 FreeBSD 上：</span>
fs<span class="sh_symbol">.</span><span class="sh_function">readFile</span><span class="sh_symbol">(</span><span class="sh_string">'&lt;目录&gt;'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">,</span> data<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_comment">// =&gt; null, &lt;data&gt;</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<p><code>fs.readFile()</code> 函数会缓冲整个文件。
为了最小化内存成本，尽可能通过 <code>fs.createReadStream()</code> 进行流式传输。</p>
<h3>文件描述符<span><a class="mark" href="#fs_file_descriptors_1" id="fs_file_descriptors_1">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/file_descriptors_1.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/928jN5" class="tip_en">提交修改</a></p>
<ol>
<li>任何指定的文件描述符都必须支持读取。</li>
<li>如果将文件描述符指定为 <code>path</code>，则不会自动关闭它。</li>
<li>读数将从当前位置开始。例如，如果文件已经有内容 <code>'Hello World</code>' 并且使用文件描述符读取了六个字节，则使用相同文件描述符调用 <code>fs.readFile()</code> 将返回 <code>'World'</code> 而不是 <code>'Hello World'</code>。</li>
</ol>
<h2>fs.readFileSync(path[, options])<span><a class="mark" href="#fs_fs_readfilesync_path_options" id="fs_fs_readfilesync_path_options">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_readfilesync_path_options.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/bnthsP" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v5.0.0</td>
<td><p>The <code>path</code> parameter can be a file descriptor now.</p></td></tr>
<tr><td>v0.1.8</td>
<td><p><span>新增于: v0.1.8</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a> | <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> 文件名或文件描述符。</li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a> | <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a></p>
<ul>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/334hvC" target="_blank" rel="nofollow" class="type">&lt;null&gt;</a> <strong>默认值:</strong> <code>null</code>。</li>
<li><code>flag</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> 参阅<a href="http://nodejs.cn/s/JjbY8n" rel="nofollow" target="_blank">支持的文件系统标志</a>。<strong>默认值:</strong> <code>'r'</code>。</li>
</ul>
</li>
<li>返回: <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a></li>
</ul>
<p>返回 <code>path</code> 的内容。</p>
<p>有关详细信息，参阅此 API 的异步版本的文档：<a href="http://nodejs.cn/s/V4cCDF" rel="nofollow" target="_blank"><code>fs.readFile()</code></a>。</p>
<p>如果指定了 <code>encoding</code> 选项，则此函数返回字符串，否则返回 buffer。</p>
<p>与 <a href="http://nodejs.cn/s/V4cCDF" rel="nofollow" target="_blank"><code>fs.readFile()</code></a> 类似，当 <code>path</code> 是目录时， <code>fs.readFileSync()</code> 的行为是特定于平台的。</p>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_comment">// 在 macOS、Linux 和 Windows 上：</span>
fs<span class="sh_symbol">.</span><span class="sh_function">readFileSync</span><span class="sh_symbol">(</span><span class="sh_string">'&lt;目录&gt;'</span><span class="sh_symbol">);</span>
<span class="sh_comment">// =&gt; [Error: EISDIR: illegal operation on a directory, read &lt;目录&gt;]</span>

<span class="sh_comment">// 在 FreeBSD 上：</span>
fs<span class="sh_symbol">.</span><span class="sh_function">readFileSync</span><span class="sh_symbol">(</span><span class="sh_string">'&lt;目录&gt;'</span><span class="sh_symbol">);</span> <span class="sh_comment">// =&gt; &lt;data&gt;</span>
</code></pre>
<h2>fs.readlink(path[, options], callback)<span><a class="mark" href="#fs_fs_readlink_path_options_callback" id="fs_fs_readlink_path_options_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_readlink_path_options_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/jCvoaQ" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.1.31</td>
<td><p><span>新增于: v0.1.31</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a></p>
<ul>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> <strong>默认值:</strong> <code>'utf8'</code>。</li>
</ul>
</li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
<li><code>linkString</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a></li>
</ul>
</li>
</ul>
<p>异步的 <a href="http://nodejs.cn/s/CFw99K" rel="nofollow" target="_blank"><code>readlink(2)</code></a>。
回调会传入两个参数 <code>(err, linkString)</code>。</p>
<p>可选的 <code>options</code> 参数可以是指定编码的字符串，也可以是具有 <code>encoding</code> 属性的对象，该属性指定用于传递给回调的链接路径的字符编码。
如果 <code>encoding</code> 设置为 <code>'buffer'</code>，则返回的链接路径将作为 <code>Buffer</code> 对象传入。</p>
<h2>fs.readlinkSync(path[, options])<span><a class="mark" href="#fs_fs_readlinksync_path_options" id="fs_fs_readlinksync_path_options">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_readlinksync_path_options.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/2Ey8M2" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v0.1.31</td>
<td><p><span>新增于: v0.1.31</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a></p>
<ul>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> <strong>默认值:</strong> <code>'utf8'</code>。</li>
</ul>
</li>
<li>返回: <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a></li>
</ul>
<p>同步的 <a href="http://nodejs.cn/s/CFw99K" rel="nofollow" target="_blank"><code>readlink(2)</code></a>。
返回符号链接的字符串值。</p>
<p>可选的 <code>options</code> 参数可以是指定编码的字符串，也可以是具有 <code>encoding</code> 属性的对象，该属性指定用于传递给回调的链接路径的字符编码。
如果 <code>encoding</code> 设置为 <code>'buffer'</code>，则返回的链接路径将作为 <code>Buffer</code> 对象传入。</p>
<h2>fs.readSync(fd, buffer, offset, length, position)<span><a class="mark" href="#fs_fs_readsync_fd_buffer_offset_length_position" id="fs_fs_readsync_fd_buffer_offset_length_position">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_readsync_fd_buffer_offset_length_position.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/NQcoAh" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.10.0</td>
<td><p>The <code>buffer</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr>
<tr><td>v6.0.0</td>
<td><p>The <code>length</code> parameter can now be <code>0</code>.</p></td></tr>
<tr><td>v0.1.21</td>
<td><p><span>新增于: v0.1.21</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>buffer</code> <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/oh3CkV" target="_blank" rel="nofollow" class="type">&lt;TypedArray&gt;</a> | <a href="http://nodejs.cn/s/yCdVkD" target="_blank" rel="nofollow" class="type">&lt;DataView&gt;</a></li>
<li><code>offset</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>length</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>position</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li>返回: <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a></li>
</ul>
<p>返回 <code>bytesRead</code> 的数量。</p>
<p>有关详细信息，请参阅此 API 异步版本的文档：<a href="http://nodejs.cn/s/XF12iY" rel="nofollow" target="_blank"><code>fs.read()</code></a>。</p>
<h2>fs.realpath(path[, options], callback)<span><a class="mark" href="#fs_fs_realpath_path_options_callback" id="fs_fs_realpath_path_options_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_realpath_path_options_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/YdhoGM" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v8.0.0</td>
<td><p>Pipe/Socket resolve support was added.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v6.4.0</td>
<td><p>Calling <code>realpath</code> now works again for various edge cases on Windows.</p></td></tr>
<tr><td>v6.0.0</td>
<td><p>The <code>cache</code> parameter was removed.</p></td></tr>
<tr><td>v0.1.31</td>
<td><p><span>新增于: v0.1.31</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a></p>
<ul>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> <strong>默认值:</strong> <code>'utf8'</code>。</li>
</ul>
</li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
<li><code>resolvedPath</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a></li>
</ul>
</li>
</ul>
<p>通过解析 <code>.</code>、 <code>..</code> 和符号链接异步地计算规范路径名。</p>
<p>规范路径名不一定是唯一的。
硬链接和绑定装载可以通过许多路径名暴露文件系统实体。</p>
<p>此函数的行为类似于 <a href="http://nodejs.cn/s/k8V6gK" rel="nofollow" target="_blank"><code>realpath(3)</code></a>，但有一些例外</p>
<ol>
<li>
<p>在不区分大小写的文件系统上不执行大小写转换。。</p>
</li>
<li>
<p>符号链接的最大数量与平台无关，并且通常高于本地 <a href="http://nodejs.cn/s/k8V6gK" rel="nofollow" target="_blank"><code>realpath(3)</code></a> 实现支持的数量。</p>
</li>
</ol>
<p><code>callback</code> 有两个参数 <code>(err, resolvedPath)</code>。
可以使用 <code>process.cwd</code> 来解析相对路径。</p>
<p>仅支持可转换为 UTF8 字符串的路径。</p>
<p>可选的 <code>options</code> 参数可以是指定编码的字符串，也可以是具有 <code>encoding</code> 属性的对象，该属性指定用于传递给回调的路径的字符编码。
如果 <code>encoding</code> 设置为 <code>'buffer'</code>，则返回的路径将作为 <code>Buffer</code> 对象传入。</p>
<p>如果 <code>path</code> 解析为套接字或管道，则该函数将返回该对象的系统相关名称。</p>
<h2>fs.realpath.native(path[, options], callback)<span><a class="mark" href="#fs_fs_realpath_native_path_options_callback" id="fs_fs_realpath_native_path_options_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_realpath_native_path_options_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/BJWNeY" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v9.2.0</span>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a></p>
<ul>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> <strong>默认值:</strong> <code>'utf8'</code>。</li>
</ul>
</li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
<li><code>resolvedPath</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a></li>
</ul>
</li>
</ul>
<p>异步的 <a href="http://nodejs.cn/s/k8V6gK" rel="nofollow" target="_blank"><code>realpath(3)</code></a>。</p>
<p><code>callback</code> 有两个参数 <code>(err, resolvedPath)</code>。</p>
<p>仅支持可转换为 UTF8 字符串的路径。</p>
<p>可选的 <code>options</code> 参数可以是指定编码的字符串，也可以是具有 <code>encoding</code> 属性的对象，该属性指定用于传递给回调的路径的字符编码。
如果 <code>encoding</code> 设置为 <code>'buffer'</code>，则返回的路径将作为 <code>Buffer</code> 对象传入。</p>
<p>在 Linux 上，当 Node.js 与 musl libc 链接时，procfs 文件系统必须挂载在 <code>/proc</code> 上才能使此功能正常工作。
Glibc 没有这个限制。</p>
<h2>fs.realpathSync(path[, options])<span><a class="mark" href="#fs_fs_realpathsync_path_options" id="fs_fs_realpathsync_path_options">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_realpathsync_path_options.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/rVGKej" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v8.0.0</td>
<td><p>Pipe/Socket resolve support was added.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v6.4.0</td>
<td><p>Calling <code>realpathSync</code> now works again for various edge cases on Windows.</p></td></tr>
<tr><td>v6.0.0</td>
<td><p>The <code>cache</code> parameter was removed.</p></td></tr>
<tr><td>v0.1.31</td>
<td><p><span>新增于: v0.1.31</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a></p>
<ul>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> <strong>默认值:</strong> <code>'utf8'</code>。</li>
</ul>
</li>
<li>返回: <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a></li>
</ul>
<p>返回已解析的路径名。</p>
<p>有关详细信息，参阅此 API 的异步版本的文档：<a href="http://nodejs.cn/s/ML4Vqb" rel="nofollow" target="_blank"><code>fs.realpath()</code></a>。</p>
<h2>fs.realpathSync.native(path[, options])<span><a class="mark" href="#fs_fs_realpathsync_native_path_options" id="fs_fs_realpathsync_native_path_options">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_realpathsync_native_path_options.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/udEiMb" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v9.2.0</span>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a></p>
<ul>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> <strong>默认值:</strong> <code>'utf8'</code>。</li>
</ul>
</li>
<li>Returns: <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a></li>
</ul>
<p>同步的 <a href="http://nodejs.cn/s/k8V6gK" rel="nofollow" target="_blank"><code>realpath(3)</code></a>。</p>
<p>仅支持可转换为 UTF8 字符串的路径。</p>
<p>可选的 <code>options</code> 参数可以是指定编码的字符串，也可以是具有 <code>encoding</code> 属性的对象，该属性指定用于传递给回调的路径的字符编码。
如果 <code>encoding</code> 设置为 <code>'buffer'</code>，则返回的路径将作为 <code>Buffer</code> 对象传入。</p>
<p>在 Linux 上，当 Node.js 与 musl libc 链接时，procfs 文件系统必须挂载在 <code>/proc</code> 上才能使此功能正常工作。
Glibc 没有这个限制。</p>
<h2>fs.rename(oldPath, newPath, callback)<span><a class="mark" href="#fs_fs_rename_oldpath_newpath_callback" id="fs_fs_rename_oldpath_newpath_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_rename_oldpath_newpath_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/jfXzGM" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>oldPath</code> and <code>newPath</code> parameters can be WHATWG <code>URL</code> objects using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.0.2</td>
<td><p><span>新增于: v0.0.2</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>oldPath</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>newPath</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
</ul>
</li>
</ul>
<p>异步地将 <code>oldPath</code> 上的文件重命名为 <code>newPath</code> 提供的路径名。
如果 <code>newPath</code> 已存在，则覆盖它。
除了可能的异常，完成回调没有其他参数。</p>
<p>也可参阅 <a href="http://nodejs.cn/s/YbqghQ" rel="nofollow" target="_blank"><code>rename(2)</code></a>。</p>
<pre class="sh_sourceCode"><code class="language-js">fs<span class="sh_symbol">.</span><span class="sh_function">rename</span><span class="sh_symbol">(</span><span class="sh_string">'旧文件.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'新文件.txt'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
  console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span><span class="sh_string">'重命名完成'</span><span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<h2>fs.renameSync(oldPath, newPath)<span><a class="mark" href="#fs_fs_renamesync_oldpath_newpath" id="fs_fs_renamesync_oldpath_newpath">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_renamesync_oldpath_newpath.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/pyxTBE" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>oldPath</code> and <code>newPath</code> parameters can be WHATWG <code>URL</code> objects using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v0.1.21</td>
<td><p><span>新增于: v0.1.21</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>oldPath</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>newPath</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
</ul>
<p>同步的 <a href="http://nodejs.cn/s/YbqghQ" rel="nofollow" target="_blank"><code>rename(2)</code></a>。返回 <code>undefined</code>。</p>
<h2>fs.rmdir(path, callback)<span><a class="mark" href="#fs_fs_rmdir_path_callback" id="fs_fs_rmdir_path_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_rmdir_path_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/JwzHgq" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameters can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.0.2</td>
<td><p><span>新增于: v0.0.2</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
</ul>
</li>
</ul>
<p>异步的 <a href="http://nodejs.cn/s/BuUYuq" rel="nofollow" target="_blank"><code>rmdir(2)</code></a>。
除了可能的异常，完成回调没有其他参数。</p>
<p>在文件（而不是目录）上使用 <code>fs.rmdir()</code> 会导致在 Windows 上出现 <code>ENOENT</code> 错误、在 POSIX 上出现 <code>ENOTDIR</code> 错误。</p>
<h2>fs.rmdirSync(path)<span><a class="mark" href="#fs_fs_rmdirsync_path" id="fs_fs_rmdirsync_path">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_rmdirsync_path.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/PtSTE4" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameters can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v0.1.21</td>
<td><p><span>新增于: v0.1.21</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
</ul>
<p>同步的 <a href="http://nodejs.cn/s/BuUYuq" rel="nofollow" target="_blank"><code>rmdir(2)</code></a>。返回 <code>undefined</code>。</p>
<p>在文件（而不是目录）上使用 <code>fs.rmdirSync()</code> 会导致在 Windows 上出现 <code>ENOENT</code> 错误、在 POSIX 上出现 <code>ENOTDIR</code> 错误。</p>
<h2>fs.stat(path[, options], callback)<span><a class="mark" href="#fs_fs_stat_path_options_callback" id="fs_fs_stat_path_options_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_stat_path_options_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/3y2ZD7" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.5.0</td>
<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.0.2</td>
<td><p><span>新增于: v0.0.2</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a></p>
<ul>
<li><code>bigint</code> <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a> 返回的 <a href="http://nodejs.cn/s/NMuvVx" rel="nofollow" target="_blank"><code>fs.Stats</code></a> 对象中的数值是否应为 <code>bigint</code> 型。<strong>默认值:</strong> <code>false</code>。</li>
</ul>
</li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
<li><code>stats</code> <a href="http://nodejs.cn/s/NMuvVx" target="_blank" rel="nofollow" class="type">&lt;fs.Stats&gt;</a></li>
</ul>
</li>
</ul>
<p>异步的 <a href="http://nodejs.cn/s/7U6CBC" rel="nofollow" target="_blank"><code>stat(2)</code></a>。
回调有两个参数 <code>(err, stats)</code>，其中 <code>stats</code> 是 <a href="http://nodejs.cn/s/NMuvVx" rel="nofollow" target="_blank"><code>fs.Stats</code></a>。</p>
<p>如果出现错误，则 <code>err.code</code> 将是<a href="http://nodejs.cn/s/5wcdGc" rel="nofollow" target="_blank">常见系统错误</a>之一。</p>
<p>不建议在调用 <code>fs.open()</code>、 <code>fs.readFile()</code> 或 <code>fs.writeFile()</code> 之前使用 <code>fs.stat()</code> 检查文件是否存在。
而是应该直接打开、读取或写入文件，如果文件不可用则处理引发的错误。</p>
<p>要检查文件是否存在但随后并不对其进行操作，则建议使用 <a href="http://nodejs.cn/s/NCPsM3" rel="nofollow" target="_blank"><code>fs.access()</code></a>。</p>
<h2>fs.statSync(path[, options])<span><a class="mark" href="#fs_fs_statsync_path_options" id="fs_fs_statsync_path_options">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_statsync_path_options.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/HYoWFs" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.5.0</td>
<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v0.1.21</td>
<td><p><span>新增于: v0.1.21</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a></p>
<ul>
<li><code>bigint</code> <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a> 返回的 <a href="http://nodejs.cn/s/NMuvVx" rel="nofollow" target="_blank"><code>fs.Stats</code></a> 对象中的数值是否应为 <code>bigint</code> 型。<strong>默认值:</strong> <code>false</code>。</li>
</ul>
</li>
<li>返回: <a href="http://nodejs.cn/s/NMuvVx" target="_blank" rel="nofollow" class="type">&lt;fs.Stats&gt;</a></li>
</ul>
<p>同步的 <a href="http://nodejs.cn/s/7U6CBC" rel="nofollow" target="_blank"><code>stat(2)</code></a>。</p>
<h2>fs.symlink(target, path[, type], callback)<span><a class="mark" href="#fs_fs_symlink_target_path_type_callback" id="fs_fs_symlink_target_path_type_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_symlink_target_path_type_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/JBY7tQ" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>target</code> and <code>path</code> parameters can be WHATWG <code>URL</code> objects using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v0.1.31</td>
<td><p><span>新增于: v0.1.31</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>target</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>type</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> <strong>默认值:</strong> <code>'file'</code>。</li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
</ul>
</li>
</ul>
<p>异步的 <a href="http://nodejs.cn/s/th7NwY" rel="nofollow" target="_blank"><code>symlink(2)</code></a>。
除了可能的异常，完成回调没有其他参数。
<code>type</code> 参数可以设置为 <code>'dir'</code>、 <code>'file'</code> 或 <code>'junction'</code>，并且仅在 Windows 上可用（在其他平台上被忽略）。
Windows 上使用 <code>'junction'</code> 要求目标路径是绝对路径。
当使用 <code>'junction'</code> 时， <code>target</code> 参数将自动标准化为绝对路径。</p>
<p>示例：</p>
<pre class="sh_sourceCode"><code class="language-js">fs<span class="sh_symbol">.</span><span class="sh_function">symlink</span><span class="sh_symbol">(</span><span class="sh_string">'./foo'</span><span class="sh_symbol">,</span> <span class="sh_string">'./new-port'</span><span class="sh_symbol">,</span> callback<span class="sh_symbol">);</span>
</code></pre>
<p>它创建了一个名为 "new-port" 且指向 "foo" 的符号链接。</p>
<h2>fs.symlinkSync(target, path[, type])<span><a class="mark" href="#fs_fs_symlinksync_target_path_type" id="fs_fs_symlinksync_target_path_type">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_symlinksync_target_path_type.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/xjxGRf" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>target</code> and <code>path</code> parameters can be WHATWG <code>URL</code> objects using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v0.1.31</td>
<td><p><span>新增于: v0.1.31</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>target</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>type</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> <strong>默认值:</strong> <code>'file'</code>。</li>
</ul>
<p>返回 <code>undefined</code>。</p>
<p>有关详细信息，参阅此 API 的异步版本的文档：<a href="http://nodejs.cn/s/nwAtfE" rel="nofollow" target="_blank"><code>fs.symlink()</code></a>。</p>
<h2>fs.truncate(path[, len], callback)<span><a class="mark" href="#fs_fs_truncate_path_len_callback" id="fs_fs_truncate_path_len_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_truncate_path_len_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/bXTkBS" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.8.6</td>
<td><p><span>新增于: v0.8.6</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>len</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <strong>默认值:</strong> <code>0</code>。</li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
</ul>
</li>
</ul>
<p>异步的 <a href="http://nodejs.cn/s/9o417s" rel="nofollow" target="_blank"><code>truncate(2)</code></a>。
除了可能的异常，完成回调没有其他参数。
文件描述符也可以作为第一个参数传入。 
在这种情况下，调用 <code>fs.ftruncate()</code>。</p>
<p>不推荐传入文件描述符，可能导致将来抛出错误。</p>
<h2>fs.truncateSync(path[, len])<span><a class="mark" href="#fs_fs_truncatesync_path_len" id="fs_fs_truncatesync_path_len">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_truncatesync_path_len.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/tjzQDc" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.8.6</span>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>len</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <strong>默认值:</strong> <code>0</code>。</li>
</ul>
<p>同步的 <a href="http://nodejs.cn/s/9o417s" rel="nofollow" target="_blank"><code>truncate(2)</code></a>。
返回 <code>undefined</code>。
文件描述符也可以作为第一个参数传入。
在这种情况下，调用 <code>fs.ftruncateSync()</code>。</p>
<p>不推荐传入文件描述符，可能导致将来抛出错误。</p>
<h2>fs.unlink(path, callback)<span><a class="mark" href="#fs_fs_unlink_path_callback" id="fs_fs_unlink_path_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_unlink_path_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/tdeBXb" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.0.2</td>
<td><p><span>新增于: v0.0.2</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
</ul>
</li>
</ul>
<p>异步地删除文件或符号链接。
除了可能的异常，完成回调没有其他参数。</p>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_comment">// 假设 'path/file.txt' 是常规文件。</span>
fs<span class="sh_symbol">.</span><span class="sh_function">unlink</span><span class="sh_symbol">(</span><span class="sh_string">'path/file.txt'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
  console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span><span class="sh_string">'文件已删除'</span><span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<p><code>fs.unlink()</code> 不能用于目录。
要删除目录，则使用 <a href="http://nodejs.cn/s/4snHRH" rel="nofollow" target="_blank"><code>fs.rmdir()</code></a>。</p>
<p>也可参阅 <a href="http://nodejs.cn/s/gjRRsM" rel="nofollow" target="_blank"><code>unlink(2)</code></a>。</p>
<h2>fs.unlinkSync(path)<span><a class="mark" href="#fs_fs_unlinksync_path" id="fs_fs_unlinksync_path">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_unlinksync_path.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/byoH4m" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v0.1.21</td>
<td><p><span>新增于: v0.1.21</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
</ul>
<p>同步的 <a href="http://nodejs.cn/s/gjRRsM" rel="nofollow" target="_blank"><code>unlink(2)</code></a>。返回 <code>undefined</code>。</p>
<h2>fs.unwatchFile(filename[, listener])<span><a class="mark" href="#fs_fs_unwatchfile_filename_listener" id="fs_fs_unwatchfile_filename_listener">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_unwatchfile_filename_listener.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/rbzdc3" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<span>新增于: v0.1.31</span>
</div>
<ul>
<li><code>filename</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>listener</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a> 可选的，之前使用 <code>fs.watchFile()</code> 绑定的监听器。</li>
</ul>
<p>停止监视 <code>filename</code> 的变化。
如果指定了 <code>listener</code>，则仅移除此特定监听器，否则，将移除所有监听器，从而停止监视 <code>filename</code>。</p>
<p>对未被监视的文件名调用 <code>fs.unwatchFile()</code> 将是空操作，而不是错误。</p>
<p>使用 <a href="http://nodejs.cn/s/h2QY7Q" rel="nofollow" target="_blank"><code>fs.watch()</code></a> 比 <code>fs.watchFile()</code> 和 <code>fs.unwatchFile()</code> 更高效。
应尽可能使用 <code>fs.watch()</code> 代替 <code>fs.watchFile()</code> 和 <code>fs.unwatchFile()</code>。</p>
<h2>fs.utimes(path, atime, mtime, callback)<span><a class="mark" href="#fs_fs_utimes_path_atime_mtime_callback" id="fs_fs_utimes_path_atime_mtime_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_utimes_path_atime_mtime_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/kqK4SM" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v8.0.0</td>
<td><p><code>NaN</code>, <code>Infinity</code>, and <code>-Infinity</code> are no longer valid time specifiers.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v4.1.0</td>
<td><p>Numeric strings, <code>NaN</code> and <code>Infinity</code> are now allowed time specifiers.</p></td></tr>
<tr><td>v0.4.2</td>
<td><p><span>新增于: v0.4.2</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>atime</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> | <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/A9TMoa" target="_blank" rel="nofollow" class="type">&lt;Date&gt;</a></li>
<li><code>mtime</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> | <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/A9TMoa" target="_blank" rel="nofollow" class="type">&lt;Date&gt;</a></li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
</ul>
</li>
</ul>
<p>更改 <code>path</code> 指向的对象的文件系统时间戳。</p>
<p><code>atime</code> 和 <code>mtime</code> 参数遵循以下规则：</p>
<ul>
<li>值可以是表示 Unix 纪元时间的数字、 <code>Date</code> 对象、或类似 <code>'123456789.0'</code> 的数值字符串。</li>
<li>如果该值无法转换为数值、或值为 <code>NaN</code>、 <code>Infinity</code> 或 <code>-Infinity</code>，则抛出错误。</li>
</ul>
<h2>fs.utimesSync(path, atime, mtime)<span><a class="mark" href="#fs_fs_utimessync_path_atime_mtime" id="fs_fs_utimessync_path_atime_mtime">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_utimessync_path_atime_mtime.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/TUYH1r" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v8.0.0</td>
<td><p><code>NaN</code>, <code>Infinity</code>, and <code>-Infinity</code> are no longer valid time specifiers.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v4.1.0</td>
<td><p>Numeric strings, <code>NaN</code> and <code>Infinity</code> are now allowed time specifiers.</p></td></tr>
<tr><td>v0.4.2</td>
<td><p><span>新增于: v0.4.2</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>atime</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>mtime</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
</ul>
<p>返回 <code>undefined</code>。</p>
<p>有关详细信息，参阅此 API 的异步版本的文档：<a href="http://nodejs.cn/s/UxAPRV" rel="nofollow" target="_blank"><code>fs.utimes()</code></a>。</p>
<h2>fs.watch(filename[, options][, listener])<span><a class="mark" href="#fs_fs_watch_filename_options_listener" id="fs_fs_watch_filename_options_listener">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_watch_filename_options_listener.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/XW2dUR" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>filename</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The passed <code>options</code> object will never be modified.</p></td></tr>
<tr><td>v0.5.10</td>
<td><p><span>新增于: v0.5.10</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>filename</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a></p>
<ul>
<li><code>persistent</code> <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a> 指示如果文件已正被监视，进程是否应继续运行。<strong>默认值:</strong> <code>true</code>。</li>
<li><code>recursive</code> <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a> 指示应该监视所有子目录，还是仅监视当前目录。这适用于监视目录时，并且仅适用于受支持的平台（参阅<a href="http://nodejs.cn/s/PJEx13" rel="nofollow" target="_blank">注意事项</a>）。<strong>默认值:</strong> <code>false</code>。</li>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> 指定用于传给监听器的文件名的字符编码。<strong>默认值:</strong> <code>'utf8'</code>。</li>
</ul>
</li>
<li>
<p><code>listener</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a> | <a href="http://nodejs.cn/s/8ym6ow" target="_blank" rel="nofollow" class="type">&lt;undefined&gt;</a> <strong>默认值:</strong> <code>undefined</code>。</p>
<ul>
<li><code>eventType</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a></li>
<li><code>filename</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a></li>
</ul>
</li>
<li>返回: <a href="http://nodejs.cn/s/8XNtAD" target="_blank" rel="nofollow" class="type">&lt;fs.FSWatcher&gt;</a></li>
</ul>
<p>监视 <code>filename</code> 的更改，其中 <code>filename</code> 是文件或目录。</p>
<p>第二个参数是可选的。
如果 <code>options</code> 传入字符串，则它指定 <code>encoding</code>。
否则， <code>options</code> 应传入对象。</p>
<p>监听器回调有两个参数 <code>(eventType, filename)</code>。
<code>eventType</code> 是 <code>'rename'</code> 或 <code>'change'</code>， <code>filename</code> 是触发事件的文件的名称。</p>
<p>在大多数平台上，每当文件名在目录中出现或消失时，就会触发 <code>'rename'</code> 事件。</p>
<p>监听器回调绑定在由 <a href="http://nodejs.cn/s/8XNtAD" rel="nofollow" target="_blank"><code>fs.FSWatcher</code></a> 触发的 <code>'change'</code> 事件上，但它与 <code>eventType</code> 的 <code>'change'</code> 值不是一回事。</p>
<h3>注意事项<span><a class="mark" href="#fs_caveats" id="fs_caveats">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/caveats.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/5YaDvF" class="tip_en">提交修改</a></p>
<!--type=misc-->
<p><code>fs.watch</code> 的 API 在各个平台上并非 100％ 一致，在某些情况下不可用。</p>
<p>仅在 macOS 和 Windows 上支持 <code>recursive</code> 选项。</p>
<h4>可用性<span><a class="mark" href="#fs_availability" id="fs_availability">#</a></span></h4><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/availability.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/GD6wtT" class="tip_en">提交修改</a></p>
<!--type=misc-->
<p>此特性取决于底层操作系统，提供了一种通知文件系统更改的方法。</p>
<ul>
<li>在 Linux 系统上，使用 <a href="http://nodejs.cn/s/d7HYeP" rel="nofollow" target="_blank"><code>inotify(7)</code></a>。</li>
<li>在 BSD 系统上，使用 <a href="http://nodejs.cn/s/sKFUBp" rel="nofollow" target="_blank"><code>kqueue(2)</code></a>。</li>
<li>在 macOS 系统上，对文件使用 <a href="http://nodejs.cn/s/sKFUBp" rel="nofollow" target="_blank"><code>kqueue(2)</code></a>，对目录使用 <a href="http://nodejs.cn/s/Asxgry" rel="nofollow" target="_blank"><code>FSEvents</code></a>。</li>
<li>在 SunOS 系统上（包括 Solaris 和 SmartOS），使用<a href="http://nodejs.cn/s/yqJ48A" rel="nofollow" target="_blank">事件端口</a>。</li>
<li>在 Windows 系统上，此特性取决于 <a href="http://nodejs.cn/s/Tbrfbe" rel="nofollow" target="_blank"><code>ReadDirectoryChangesW</code></a>。</li>
<li>在 Aix 系统上，此特性取决于 <a href="http://nodejs.cn/s/uAha9z" rel="nofollow" target="_blank"><code>AHAFS</code></a> 必须启动。</li>
</ul>
<p>如果底层功能由于某些原因不可用，则 <code>fs.watch</code> 将无法运行。
例如，当使用虚拟化软件（如 Vagrant、Docker 等）时，在网络文件系统（NFS、SMB 等）或主文件系统上监视文件或目录可能是不可靠的，在某些情况下也是不可能的。</p>
<p>仍然可以使用 <code>fs.watchFile()</code>，因为它使用 stat 轮询 ，但这种方法较慢且不太可靠。</p>
<h4>索引节点<span><a class="mark" href="#fs_inodes" id="fs_inodes">#</a></span></h4><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/inodes.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/A47M8U" class="tip_en">提交修改</a></p>
<!--type=misc-->
<p>在 Linux 或 macOS 系统上， <code>fs.watch()</code> 解析路径到<a href="http://nodejs.cn/s/ofGTst" rel="nofollow" target="_blank">索引节点</a>并监视该索引节点。
如果删除并重新创建监视的路径，则会为其分配一个新的索引节点。
监视器会因删除而触发事件，但会继续监视原始的索引节点。
不会因新建索引节点而触发事件。
这是预期的行为。</p>
<p>AIX 文件在文件的生命周期中保留相同的索引节点。
在 AIX 上保存和关闭监视的文件将导致两个通知（一个用于添加新内容，一个用于截断）。</p>
<h4>文件名参数<span><a class="mark" href="#fs_filename_argument" id="fs_filename_argument">#</a></span></h4><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/filename_argument.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/FVJMbB" class="tip_en">提交修改</a></p>
<!--type=misc-->
<p>仅在 Linux、macOS、Windows 和 AIX 上支持在回调中提供 <code>filename</code> 参数。
即使在支持的平台上，也不总是保证提供 <code>filename</code>。
因此，不要假设在回调中始终提供 <code>filename</code> 参数，并且如果它为 <code>null</code> 则需要一些后备逻辑。</p>
<pre class="sh_sourceCode"><code class="language-js">fs<span class="sh_symbol">.</span><span class="sh_function">watch</span><span class="sh_symbol">(</span><span class="sh_string">'somedir'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>eventType<span class="sh_symbol">,</span> filename<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span>`事件类型是<span class="sh_symbol">:</span> $<span class="sh_cbracket">{</span>eventType<span class="sh_cbracket">}</span>`<span class="sh_symbol">);</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>filename<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span>
    console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span>`提供的文件名<span class="sh_symbol">:</span> $<span class="sh_cbracket">{</span>filename<span class="sh_cbracket">}</span>`<span class="sh_symbol">);</span>
  <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_cbracket">{</span>
    console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span><span class="sh_string">'文件名未提供'</span><span class="sh_symbol">);</span>
  <span class="sh_cbracket">}</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<h2>fs.watchFile(filename[, options], listener)<span><a class="mark" href="#fs_fs_watchfile_filename_options_listener" id="fs_fs_watchfile_filename_options_listener">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_watchfile_filename_options_listener.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/QXW4N2" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>filename</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v0.1.31</td>
<td><p><span>新增于: v0.1.31</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>filename</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a></p>
<ul>
<li><code>persistent</code> <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a> <strong>默认值:</strong> <code>true</code>。</li>
<li><code>interval</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <strong>默认值:</strong> <code>5007</code>。</li>
</ul>
</li>
<li>
<p><code>listener</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>current</code> <a href="http://nodejs.cn/s/NMuvVx" target="_blank" rel="nofollow" class="type">&lt;fs.Stats&gt;</a></li>
<li><code>previous</code> <a href="http://nodejs.cn/s/NMuvVx" target="_blank" rel="nofollow" class="type">&lt;fs.Stats&gt;</a></li>
</ul>
</li>
</ul>
<p>监视 <code>filename</code> 的更改。
每当访问文件时都会调用 <code>listener</code> 回调。</p>
<p><code>options</code> 参数可以省略。
如果提供，则它应该是一个对象。
<code>options</code> 对象可以包含一个名为 <code>persistent</code> 的布尔值，指示当文件正在被监视时，进程是否应该继续运行。
<code>options</code> 对象可以指定 <code>interval</code> 属性，指示轮询目标的频率（以毫秒为单位）。</p>
<p><code>listener</code> 有两个参数，当前的 stat 对象和之前的 stat 对象：</p>
<pre class="sh_sourceCode"><code class="language-js">fs<span class="sh_symbol">.</span><span class="sh_function">watchFile</span><span class="sh_symbol">(</span><span class="sh_string">'message.text'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>curr<span class="sh_symbol">,</span> prev<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span>`当前的最近修改时间是<span class="sh_symbol">:</span> $<span class="sh_cbracket">{</span>curr<span class="sh_symbol">.</span>mtime<span class="sh_cbracket">}</span>`<span class="sh_symbol">);</span>
  console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span>`之前的最近修改时间是<span class="sh_symbol">:</span> $<span class="sh_cbracket">{</span>prev<span class="sh_symbol">.</span>mtime<span class="sh_cbracket">}</span>`<span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<p>这些 stat 对象是 <code>fs.Stat</code> 的实例。</p>
<p>要在修改文件（而不仅仅是访问）时收到通知，则需要比较 <code>curr.mtime</code> 和 <code>prev.mtime</code>。</p>
<p>当 <code>fs.watchFile</code> 操作导致 <code>ENOENT</code> 错误时，它将调用一次监听器，并将所有字段置零（或将日期设为 Unix 纪元）。
在 Windows 中， <code>blksize</code> 和 <code>blocks</code> 字段会是 <code>undefined</code> 而不是零。
如果文件是在那之后创建的，则监听器会被再次调用，且带上最新的 stat 对象。
这是 v0.10 之后的功能变化。</p>
<p>使用 <a href="http://nodejs.cn/s/h2QY7Q" rel="nofollow" target="_blank"><code>fs.watch()</code></a> 比 <code>fs.watchFile</code> 和 <code>fs.unwatchFile</code> 更高效。
应尽可能使用 <code>fs.watch</code> 代替 <code>fs.watchFile</code> 和 <code>fs.unwatchFile</code>。</p>
<p>当 <code>fs.watchFile()</code> 正在监视的文件消失并重新出现时，第二次回调事件（文件重新出现）返回的 <code>previousStat</code> 会与第一次回调事件（文件消失）返回的 <code>previousStat</code> 相同。</p>
<p>这种情况发生在:</p>
<ul>
<li>文件被删除，然后又恢复。</li>
<li>文件被重命名两次，且第二次重命名回其原来的名称。</li>
</ul>
<h2>fs.write(fd, buffer[, offset[, length[, position]]], callback)<span><a class="mark" href="#fs_fs_write_fd_buffer_offset_length_position_callback" id="fs_fs_write_fd_buffer_offset_length_position_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_write_fd_buffer_offset_length_position_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/9VabQT" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.10.0</td>
<td><p>The <code>buffer</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code></p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.4.0</td>
<td><p>The <code>buffer</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr>
<tr><td>v7.2.0</td>
<td><p>The <code>offset</code> and <code>length</code> parameters are optional now.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.0.2</td>
<td><p><span>新增于: v0.0.2</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>buffer</code> <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/oh3CkV" target="_blank" rel="nofollow" class="type">&lt;TypedArray&gt;</a> | <a href="http://nodejs.cn/s/yCdVkD" target="_blank" rel="nofollow" class="type">&lt;DataView&gt;</a></li>
<li><code>offset</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>length</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>position</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
<li><code>bytesWritten</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>buffer</code> <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/oh3CkV" target="_blank" rel="nofollow" class="type">&lt;TypedArray&gt;</a> | <a href="http://nodejs.cn/s/yCdVkD" target="_blank" rel="nofollow" class="type">&lt;DataView&gt;</a></li>
</ul>
</li>
</ul>
<p>将 <code>buffer</code> 写入到 <code>fd</code> 指定的文件。</p>
<p><code>offset</code> 决定 <code>buffer</code> 中要被写入的部位， <code>length</code> 是一个整数，指定要写入的字节数。</p>
<p><code>position</code> 指定文件开头的偏移量（数据应该被写入的位置）。
如果 <code>typeof position !== 'number'</code>，则数据会被写入当前的位置。
参阅 <a href="http://nodejs.cn/s/2qJ9AM" rel="nofollow" target="_blank"><code>pwrite(2)</code></a>。</p>
<p>回调有三个参数 <code>(err, bytesWritten, buffer)</code>，其中 <code>bytesWritten</code> 指定 <code>buffer</code> 中被写入的字节数。</p>
<p>如果调用此方法的 <a href="http://nodejs.cn/s/DGMNHh" rel="nofollow" target="_blank"><code>util.promisify()</code></a> 版本，则返回的 <code>Promise</code> 会返回具有 <code>bytesWritten</code> 和 <code>buffer</code> 属性的 <code>Object</code>。</p>
<p>在同一个文件上多次使用 <code>fs.write()</code> 且不等待回调是不安全的。
对于这种情况，建议使用 <a href="http://nodejs.cn/s/VdSJQa" rel="nofollow" target="_blank"><code>fs.createWriteStream()</code></a>。</p>
<p>在 Linux 上，当以追加模式打开文件时，写入无法指定位置。
内核会忽略位置参数，并始终将数据追加到文件的末尾。</p>
<h2>fs.write(fd, string[, position[, encoding]], callback)<span><a class="mark" href="#fs_fs_write_fd_string_position_encoding_callback" id="fs_fs_write_fd_string_position_encoding_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_write_fd_string_position_encoding_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/bgPpbG" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.2.0</td>
<td><p>The <code>position</code> parameter is optional now.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.11.5</td>
<td><p><span>新增于: v0.11.5</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>string</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a></li>
<li><code>position</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> <strong>默认值:</strong> <code>'utf8'</code>。</li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
<li><code>written</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>string</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a></li>
</ul>
</li>
</ul>
<p>将 <code>string</code> 写入到 <code>fd</code> 指定的文件。
如果 <code>string</code> 不是一个字符串，则该值会被强制转换为字符串。</p>
<p><code>position</code> 指定文件开头的偏移量（数据应该被写入的位置）。
如果 <code>typeof position !== 'number'</code>，则数据会被写入当前的位置。
参阅 <a href="http://nodejs.cn/s/2qJ9AM" rel="nofollow" target="_blank"><code>pwrite(2)</code></a>。</p>
<p><code>encoding</code> 是期望的字符串编码。</p>
<p>回调会接收到参数 <code>(err, written, string)</code>，其中 <code>written</code> 指定传入的字符串中被要求写入的字节数。
被写入的字节数不一定与被写入的字符串字符数相同。
参阅 <a href="http://nodejs.cn/s/J9wTov" rel="nofollow" target="_blank"><code>Buffer.byteLength</code></a>。</p>
<p>在同一个文件上多次使用 <code>fs.write()</code> 且不等待回调是不安全的。
对于这种情况，建议使用 <a href="http://nodejs.cn/s/VdSJQa" rel="nofollow" target="_blank"><code>fs.createWriteStream()</code></a>。</p>
<p>在 Linux 上，当以追加模式打开文件时，写入无法指定位置。
内核会忽略位置参数，并始终将数据追加到文件的末尾。</p>
<p>在 Windows 上，如果文件描述符连接到控制台（例如 <code>fd == 1</code> 或 <code>stdout</code>），则无论使用何种编码，包含非 ASCII 字符的字符串默认情况下都不会被正确地渲染。
通过使用 <code>chcp 65001</code> 命令更改活动的代码页，可以将控制台配置为正确地渲染 UTF-8。
详见 <a href="http://nodejs.cn/s/NAMZkR" rel="nofollow" target="_blank">chcp</a> 文档。</p>
<h2>fs.writeFile(file, data[, options], callback)<span><a class="mark" href="#fs_fs_writefile_file_data_options_callback" id="fs_fs_writefile_file_data_options_callback">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_writefile_file_data_options_callback.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/W1hxkG" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.10.0</td>
<td><p>The <code>data</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.4.0</td>
<td><p>The <code>data</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v5.0.0</td>
<td><p>The <code>file</code> parameter can be a file descriptor now.</p></td></tr>
<tr><td>v0.1.29</td>
<td><p><span>新增于: v0.1.29</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>file</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a> | <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> 文件名或文件描述符。</li>
<li><code>data</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/oh3CkV" target="_blank" rel="nofollow" class="type">&lt;TypedArray&gt;</a> | <a href="http://nodejs.cn/s/yCdVkD" target="_blank" rel="nofollow" class="type">&lt;DataView&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a> | <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a></p>
<ul>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/334hvC" target="_blank" rel="nofollow" class="type">&lt;null&gt;</a> <strong>默认值:</strong> <code>'utf8'</code>。</li>
<li><code>mode</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <strong>默认值:</strong> <code>0o666</code>。</li>
<li><code>flag</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> 参阅<a href="http://nodejs.cn/s/JjbY8n" rel="nofollow" target="_blank">支持的文件系统标志</a>。<strong>默认值:</strong> <code>'w'</code>。</li>
</ul>
</li>
<li>
<p><code>callback</code> <a href="http://nodejs.cn/s/ceTQa6" target="_blank" rel="nofollow" class="type">&lt;Function&gt;</a></p>
<ul>
<li><code>err</code> <a href="http://nodejs.cn/s/qZ873x" target="_blank" rel="nofollow" class="type">&lt;Error&gt;</a></li>
</ul>
</li>
</ul>
<p>异步地将数据写入到一个文件，如果文件已存在则覆盖该文件。
<code>data</code> 可以是字符串或 buffer。</p>
<p>如果 <code>data</code> 是一个 buffer，则 <code>encoding</code> 选项会被忽略。</p>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_keyword">const</span> data <span class="sh_symbol">=</span> <span class="sh_keyword">new</span> <span class="sh_function">Uint8Array</span><span class="sh_symbol">(</span>Buffer<span class="sh_symbol">.</span><span class="sh_function">from</span><span class="sh_symbol">(</span><span class="sh_string">'Node.js中文网'</span><span class="sh_symbol">));</span>
fs<span class="sh_symbol">.</span><span class="sh_function">writeFile</span><span class="sh_symbol">(</span><span class="sh_string">'文件.txt'</span><span class="sh_symbol">,</span> data<span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">)</span> <span class="sh_keyword">throw</span> err<span class="sh_symbol">;</span>
  console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span><span class="sh_string">'文件已被保存'</span><span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<p>如果 <code>options</code> 是一个字符串，则它指定字符编码：</p>
<pre class="sh_sourceCode"><code class="language-js">fs<span class="sh_symbol">.</span><span class="sh_function">writeFile</span><span class="sh_symbol">(</span><span class="sh_string">'文件.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'Node.js中文网'</span><span class="sh_symbol">,</span> <span class="sh_string">'utf8'</span><span class="sh_symbol">,</span> callback<span class="sh_symbol">);</span>
</code></pre>
<p>在同一个文件上多次使用 <code>fs.writeFile()</code> 且不等待回调是不安全的。
对于这种情况，建议使用 <a href="http://nodejs.cn/s/VdSJQa" rel="nofollow" target="_blank"><code>fs.createWriteStream()</code></a>。</p>
<h3>文件描述符<span><a class="mark" href="#fs_file_descriptors_2" id="fs_file_descriptors_2">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/file_descriptors_2.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/4CExjP" class="tip_en">提交修改</a></p>
<ol>
<li>任何指定的文件描述符都必须支持写入。</li>
<li>如果将文件描述符指定为 <code>file</code>，则不会自动关闭它。</li>
<li>写入将从文件的开头开始。例如，如果文件已经有内容 <code>'Hello World</code>' 并且新写入的内容是 <code>'Aloha'</code>，则该文件的内容将是 <code>'Aloha World'</code> 而不仅仅是 <code>'Aloha'</code>。</li>
</ol>
<h2>fs.writeFileSync(file, data[, options])<span><a class="mark" href="#fs_fs_writefilesync_file_data_options" id="fs_fs_writefilesync_file_data_options">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_writefilesync_file_data_options.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/BKiuVq" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.10.0</td>
<td><p>The <code>data</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr>
<tr><td>v7.4.0</td>
<td><p>The <code>data</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr>
<tr><td>v5.0.0</td>
<td><p>The <code>file</code> parameter can be a file descriptor now.</p></td></tr>
<tr><td>v0.1.29</td>
<td><p><span>新增于: v0.1.29</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>file</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a> | <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> 文件名或文件描述符。</li>
<li><code>data</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/oh3CkV" target="_blank" rel="nofollow" class="type">&lt;TypedArray&gt;</a> | <a href="http://nodejs.cn/s/yCdVkD" target="_blank" rel="nofollow" class="type">&lt;DataView&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a> | <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a></p>
<ul>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/334hvC" target="_blank" rel="nofollow" class="type">&lt;null&gt;</a> <strong>默认值:</strong> <code>'utf8'</code>。</li>
<li><code>mode</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <strong>默认值:</strong> <code>0o666</code>。</li>
<li><code>flag</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> 参阅<a href="http://nodejs.cn/s/JjbY8n" rel="nofollow" target="_blank">支持的文件系统标志</a>。<strong>默认值:</strong> <code>'w'</code>。</li>
</ul>
</li>
</ul>
<p>返回 <code>undefined</code>。</p>
<p>有关详细信息，参阅此 API 的异步版本的文档：<a href="http://nodejs.cn/s/rnLrUA" rel="nofollow" target="_blank"><code>fs.writeFile()</code></a>。</p>
<h2>fs.writeSync(fd, buffer[, offset[, length[, position]]])<span><a class="mark" href="#fs_fs_writesync_fd_buffer_offset_length_position" id="fs_fs_writesync_fd_buffer_offset_length_position">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_writesync_fd_buffer_offset_length_position.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/KbYCXA" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.10.0</td>
<td><p>The <code>buffer</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr>
<tr><td>v7.4.0</td>
<td><p>The <code>buffer</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr>
<tr><td>v7.2.0</td>
<td><p>The <code>offset</code> and <code>length</code> parameters are optional now.</p></td></tr>
<tr><td>v0.1.21</td>
<td><p><span>新增于: v0.1.21</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>buffer</code> <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/oh3CkV" target="_blank" rel="nofollow" class="type">&lt;TypedArray&gt;</a> | <a href="http://nodejs.cn/s/yCdVkD" target="_blank" rel="nofollow" class="type">&lt;DataView&gt;</a></li>
<li><code>offset</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>length</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>position</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li>返回: <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> 写入的字节数。</li>
</ul>
<p>有关详细信息，参阅此 API 的异步版本的文档：<a href="http://nodejs.cn/s/tcfaJD" rel="nofollow" target="_blank"><code>fs.write(fd, buffer...)</code></a>。</p>
<h2>fs.writeSync(fd, string[, position[, encoding]])<span><a class="mark" href="#fs_fs_writesync_fd_string_position_encoding" id="fs_fs_writesync_fd_string_position_encoding">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_writesync_fd_string_position_encoding.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/AVtHgf" class="tip_en">提交修改</a></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v7.2.0</td>
<td><p>The <code>position</code> parameter is optional now.</p></td></tr>
<tr><td>v0.11.5</td>
<td><p><span>新增于: v0.11.5</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>string</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a></li>
<li><code>position</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a></li>
<li>返回: <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> 写入的字节数。</li>
</ul>
<p>有关详细信息，参阅此 API 的异步版本的文档： <a href="http://nodejs.cn/s/hJ75C3" rel="nofollow" target="_blank"><code>fs.write(fd, string...)</code></a>。</p>
<h2>fs 的 Promise API<span><a class="mark" href="#fs_fs_promises_api" id="fs_fs_promises_api">#</a></span></h2><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<p></p><div class="api_stability api_stability_1"><a target="_blank" rel="nofollow" href="http://nodejs.cn/api/documentation.html#documentation_stability_index">稳定性: 1</a> - 试验</div><p></p>
<p>The <code>fs.promises</code> API provides an alternative set of asynchronous file system
methods that return <code>Promise</code> objects rather than using callbacks. The
API is accessible via <code>require('fs').promises</code>.</p>
<h3>FileHandle 类<span><a class="mark" href="#fs_class_filehandle" id="fs_class_filehandle">#</a></span></h3><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<p>A <code>FileHandle</code> object is a wrapper for a numeric file descriptor.
Instances of <code>FileHandle</code> are distinct from numeric file descriptors
in that, if the <code>FileHandle</code> is not explicitly closed using the
<code>filehandle.close()</code> method, they will automatically close the file descriptor
and will emit a process warning, thereby helping to prevent memory leaks.</p>
<p>Instances of the <code>FileHandle</code> object are created internally by the
<code>fsPromises.open()</code> method.</p>
<p>Unlike the callback-based API (<code>fs.fstat()</code>, <code>fs.fchown()</code>, <code>fs.fchmod()</code>, and
so on), a numeric file descriptor is not used by the promise-based API. Instead,
the promise-based API uses the <code>FileHandle</code> class in order to help avoid
accidental leaking of unclosed file descriptors after a <code>Promise</code> is resolved or
rejected.</p>
<h4>filehandle.appendFile(data, options)<span><a class="mark" href="#fs_filehandle_appendfile_data_options" id="fs_filehandle_appendfile_data_options">#</a></span></h4><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li><code>data</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a> | <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a></p>
<ul>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/334hvC" target="_blank" rel="nofollow" class="type">&lt;null&gt;</a> <strong>Default:</strong> <code>'utf8'</code></li>
<li><code>mode</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <strong>Default:</strong> <code>0o666</code></li>
<li><code>flag</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> See <a href="http://nodejs.cn/s/JjbY8n" rel="nofollow" target="_blank">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'a'</code>.</li>
</ul>
</li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Asynchronously append data to this file, creating the file if it does not yet
exist. <code>data</code> can be a string or a <a href="http://nodejs.cn/s/FApxjh" rel="nofollow" target="_blank"><code>Buffer</code></a>. The <code>Promise</code> will be
resolved with no arguments upon success.</p>
<p>If <code>options</code> is a string, then it specifies the encoding.</p>
<p>The <code>FileHandle</code> must have been opened for appending.</p>
<h4>filehandle.chmod(mode)<span><a class="mark" href="#fs_filehandle_chmod_mode" id="fs_filehandle_chmod_mode">#</a></span></h4><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li><code>mode</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Modifies the permissions on the file. The <code>Promise</code> is resolved with no
arguments upon success.</p>
<h4>filehandle.chown(uid, gid)<span><a class="mark" href="#fs_filehandle_chown_uid_gid" id="fs_filehandle_chown_uid_gid">#</a></span></h4><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li><code>uid</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>gid</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Changes the ownership of the file then resolves the <code>Promise</code> with no arguments
upon success.</p>
<h4>filehandle.close()<span><a class="mark" href="#fs_filehandle_close" id="fs_filehandle_close">#</a></span></h4><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a> A <code>Promise</code> that will be resolved once the underlying
file descriptor is closed, or will be rejected if an error occurs while
closing.</li>
</ul>
<p>Closes the file descriptor.</p>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_keyword">const</span> fsPromises <span class="sh_symbol">=</span> <span class="sh_function">require</span><span class="sh_symbol">(</span><span class="sh_string">'fs'</span><span class="sh_symbol">).</span>promises<span class="sh_symbol">;</span>
async <span class="sh_keyword">function</span> <span class="sh_function">openAndClose</span><span class="sh_symbol">()</span> <span class="sh_cbracket">{</span>
  let filehandle<span class="sh_symbol">;</span>
  <span class="sh_keyword">try</span> <span class="sh_cbracket">{</span>
    filehandle <span class="sh_symbol">=</span> await fsPromises<span class="sh_symbol">.</span><span class="sh_function">open</span><span class="sh_symbol">(</span><span class="sh_string">'thefile.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'r'</span><span class="sh_symbol">);</span>
  <span class="sh_cbracket">}</span> <span class="sh_keyword">finally</span> <span class="sh_cbracket">{</span>
    <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>filehandle <span class="sh_symbol">!==</span> <span class="sh_predef_var">undefined</span><span class="sh_symbol">)</span>
      await filehandle<span class="sh_symbol">.</span><span class="sh_function">close</span><span class="sh_symbol">();</span>
  <span class="sh_cbracket">}</span>
<span class="sh_cbracket">}</span>
</code></pre>
<h4>filehandle.datasync()<span><a class="mark" href="#fs_filehandle_datasync" id="fs_filehandle_datasync">#</a></span></h4><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Asynchronous <a href="http://nodejs.cn/s/AEp6fv" rel="nofollow" target="_blank"><code>fdatasync(2)</code></a>. The <code>Promise</code> is resolved with no arguments upon
success.</p>
<h4>filehandle.fd<span><a class="mark" href="#fs_filehandle_fd" id="fs_filehandle_fd">#</a></span></h4><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li><a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> The numeric file descriptor managed by the <code>FileHandle</code> object.</li>
</ul>
<h4>filehandle.read(buffer, offset, length, position)<span><a class="mark" href="#fs_filehandle_read_buffer_offset_length_position" id="fs_filehandle_read_buffer_offset_length_position">#</a></span></h4><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li><code>buffer</code> <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/ZbDkpm" target="_blank" rel="nofollow" class="type">&lt;Uint8Array&gt;</a></li>
<li><code>offset</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>length</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>position</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Read data from the file.</p>
<p><code>buffer</code> is the buffer that the data will be written to.</p>
<p><code>offset</code> is the offset in the buffer to start writing at.</p>
<p><code>length</code> is an integer specifying the number of bytes to read.</p>
<p><code>position</code> is an argument specifying where to begin reading from in the file.
If <code>position</code> is <code>null</code>, data will be read from the current file position,
and the file position will be updated.
If <code>position</code> is an integer, the file position will remain unchanged.</p>
<p>Following successful read, the <code>Promise</code> is resolved with an object with a
<code>bytesRead</code> property specifying the number of bytes read, and a <code>buffer</code>
property that is a reference to the passed in <code>buffer</code> argument.</p>
<h4>filehandle.readFile(options)<span><a class="mark" href="#fs_filehandle_readfile_options" id="fs_filehandle_readfile_options">#</a></span></h4><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a> | <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a></p>
<ul>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/334hvC" target="_blank" rel="nofollow" class="type">&lt;null&gt;</a> <strong>Default:</strong> <code>null</code></li>
<li><code>flag</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> See <a href="http://nodejs.cn/s/JjbY8n" rel="nofollow" target="_blank">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'r'</code>.</li>
</ul>
</li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Asynchronously reads the entire contents of a file.</p>
<p>The <code>Promise</code> is resolved with the contents of the file. If no encoding is
specified (using <code>options.encoding</code>), the data is returned as a <code>Buffer</code>
object. Otherwise, the data will be a string.</p>
<p>If <code>options</code> is a string, then it specifies the encoding.</p>
<p>When the <code>path</code> is a directory, the behavior of <code>fsPromises.readFile()</code> is
platform-specific. On macOS, Linux, and Windows, the promise will be rejected
with an error. On FreeBSD, a representation of the directory's contents will be
returned.</p>
<p>The <code>FileHandle</code> has to support reading.</p>
<p>If one or more <code>filehandle.read()</code> calls are made on a file handle and then a
<code>filehandle.readFile()</code> call is made, the data will be read from the current
position till the end of the file. It doesn't always read from the beginning
of the file.</p>
<h4>filehandle.stat([options])<span><a class="mark" href="#fs_filehandle_stat_options" id="fs_filehandle_stat_options">#</a></span></h4><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.5.0</td>
<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p><span>新增于: v10.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a></p>
<ul>
<li><code>bigint</code> <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a> Whether the numeric values in the returned
<a href="http://nodejs.cn/s/NMuvVx" rel="nofollow" target="_blank"><code>fs.Stats</code></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li>
</ul>
</li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Retrieves the <a href="http://nodejs.cn/s/NMuvVx" rel="nofollow" target="_blank"><code>fs.Stats</code></a> for the file.</p>
<h4>filehandle.sync()<span><a class="mark" href="#fs_filehandle_sync" id="fs_filehandle_sync">#</a></span></h4><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Asynchronous <a href="http://nodejs.cn/s/d4u3Ks" rel="nofollow" target="_blank"><code>fsync(2)</code></a>. The <code>Promise</code> is resolved with no arguments upon
success.</p>
<h4>filehandle.truncate(len)<span><a class="mark" href="#fs_filehandle_truncate_len" id="fs_filehandle_truncate_len">#</a></span></h4><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li><code>len</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <strong>Default:</strong> <code>0</code></li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Truncates the file then resolves the <code>Promise</code> with no arguments upon success.</p>
<p>If the file was larger than <code>len</code> bytes, only the first <code>len</code> bytes will be
retained in the file.</p>
<p>For example, the following program retains only the first four bytes of the
file:</p>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_keyword">const</span> fs <span class="sh_symbol">=</span> <span class="sh_function">require</span><span class="sh_symbol">(</span><span class="sh_string">'fs'</span><span class="sh_symbol">);</span>
<span class="sh_keyword">const</span> fsPromises <span class="sh_symbol">=</span> fs<span class="sh_symbol">.</span>promises<span class="sh_symbol">;</span>

console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span>fs<span class="sh_symbol">.</span><span class="sh_function">readFileSync</span><span class="sh_symbol">(</span><span class="sh_string">'temp.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'utf8'</span><span class="sh_symbol">));</span>
<span class="sh_comment">// Prints: Node.js</span>

async <span class="sh_keyword">function</span> <span class="sh_function">doTruncate</span><span class="sh_symbol">()</span> <span class="sh_cbracket">{</span>
  let filehandle <span class="sh_symbol">=</span> <span class="sh_keyword">null</span><span class="sh_symbol">;</span>
  <span class="sh_keyword">try</span> <span class="sh_cbracket">{</span>
    filehandle <span class="sh_symbol">=</span> await fsPromises<span class="sh_symbol">.</span><span class="sh_function">open</span><span class="sh_symbol">(</span><span class="sh_string">'temp.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'r+'</span><span class="sh_symbol">);</span>
    await filehandle<span class="sh_symbol">.</span><span class="sh_function">truncate</span><span class="sh_symbol">(</span><span class="sh_number">4</span><span class="sh_symbol">);</span>
  <span class="sh_cbracket">}</span> <span class="sh_keyword">finally</span> <span class="sh_cbracket">{</span>
    <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>filehandle<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span>
      <span class="sh_comment">// close the file if it is opened.</span>
      await filehandle<span class="sh_symbol">.</span><span class="sh_function">close</span><span class="sh_symbol">();</span>
    <span class="sh_cbracket">}</span>
  <span class="sh_cbracket">}</span>
  console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span>fs<span class="sh_symbol">.</span><span class="sh_function">readFileSync</span><span class="sh_symbol">(</span><span class="sh_string">'temp.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'utf8'</span><span class="sh_symbol">));</span>  <span class="sh_comment">// Prints: Node</span>
<span class="sh_cbracket">}</span>

<span class="sh_function">doTruncate</span><span class="sh_symbol">().</span><span class="sh_keyword">catch</span><span class="sh_symbol">(</span>console<span class="sh_symbol">.</span>error<span class="sh_symbol">);</span>
</code></pre>
<p>If the file previously was shorter than <code>len</code> bytes, it is extended, and the
extended part is filled with null bytes (<code>'\0'</code>):</p>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_keyword">const</span> fs <span class="sh_symbol">=</span> <span class="sh_function">require</span><span class="sh_symbol">(</span><span class="sh_string">'fs'</span><span class="sh_symbol">);</span>
<span class="sh_keyword">const</span> fsPromises <span class="sh_symbol">=</span> fs<span class="sh_symbol">.</span>promises<span class="sh_symbol">;</span>

console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span>fs<span class="sh_symbol">.</span><span class="sh_function">readFileSync</span><span class="sh_symbol">(</span><span class="sh_string">'temp.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'utf8'</span><span class="sh_symbol">));</span>
<span class="sh_comment">// Prints: Node.js</span>

async <span class="sh_keyword">function</span> <span class="sh_function">doTruncate</span><span class="sh_symbol">()</span> <span class="sh_cbracket">{</span>
  let filehandle <span class="sh_symbol">=</span> <span class="sh_keyword">null</span><span class="sh_symbol">;</span>
  <span class="sh_keyword">try</span> <span class="sh_cbracket">{</span>
    filehandle <span class="sh_symbol">=</span> await fsPromises<span class="sh_symbol">.</span><span class="sh_function">open</span><span class="sh_symbol">(</span><span class="sh_string">'temp.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'r+'</span><span class="sh_symbol">);</span>
    await filehandle<span class="sh_symbol">.</span><span class="sh_function">truncate</span><span class="sh_symbol">(</span><span class="sh_number">10</span><span class="sh_symbol">);</span>
  <span class="sh_cbracket">}</span> <span class="sh_keyword">finally</span> <span class="sh_cbracket">{</span>
    <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>filehandle<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span>
      <span class="sh_comment">// close the file if it is opened.</span>
      await filehandle<span class="sh_symbol">.</span><span class="sh_function">close</span><span class="sh_symbol">();</span>
    <span class="sh_cbracket">}</span>
  <span class="sh_cbracket">}</span>
  console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span>fs<span class="sh_symbol">.</span><span class="sh_function">readFileSync</span><span class="sh_symbol">(</span><span class="sh_string">'temp.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'utf8'</span><span class="sh_symbol">));</span>  <span class="sh_comment">// Prints Node.js\0\0\0</span>
<span class="sh_cbracket">}</span>

<span class="sh_function">doTruncate</span><span class="sh_symbol">().</span><span class="sh_keyword">catch</span><span class="sh_symbol">(</span>console<span class="sh_symbol">.</span>error<span class="sh_symbol">);</span>
</code></pre>
<p>The last three bytes are null bytes (<code>'\0'</code>), to compensate the over-truncation.</p>
<h4>filehandle.utimes(atime, mtime)<span><a class="mark" href="#fs_filehandle_utimes_atime_mtime" id="fs_filehandle_utimes_atime_mtime">#</a></span></h4><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li><code>atime</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> | <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/A9TMoa" target="_blank" rel="nofollow" class="type">&lt;Date&gt;</a></li>
<li><code>mtime</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> | <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/A9TMoa" target="_blank" rel="nofollow" class="type">&lt;Date&gt;</a></li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Change the file system timestamps of the object referenced by the <code>FileHandle</code>
then resolves the <code>Promise</code> with no arguments upon success.</p>
<p>This function does not work on AIX versions before 7.1, it will resolve the
<code>Promise</code> with an error using code <code>UV_ENOSYS</code>.</p>
<h4>filehandle.write(buffer, offset, length, position)<span><a class="mark" href="#fs_filehandle_write_buffer_offset_length_position" id="fs_filehandle_write_buffer_offset_length_position">#</a></span></h4><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li><code>buffer</code> <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/ZbDkpm" target="_blank" rel="nofollow" class="type">&lt;Uint8Array&gt;</a></li>
<li><code>offset</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>length</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>position</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Write <code>buffer</code> to the file.</p>
<p>The <code>Promise</code> is resolved with an object containing a <code>bytesWritten</code> property
identifying the number of bytes written, and a <code>buffer</code> property containing
a reference to the <code>buffer</code> written.</p>
<p><code>offset</code> determines the part of the buffer to be written, and <code>length</code> is
an integer specifying the number of bytes to write.</p>
<p><code>position</code> refers to the offset from the beginning of the file where this data
should be written. If <code>typeof position !== 'number'</code>, the data will be written
at the current position. See <a href="http://nodejs.cn/s/2qJ9AM" rel="nofollow" target="_blank"><code>pwrite(2)</code></a>.</p>
<p>It is unsafe to use <code>filehandle.write()</code> multiple times on the same file
without waiting for the <code>Promise</code> to be resolved (or rejected). For this
scenario, <a href="http://nodejs.cn/s/VdSJQa" rel="nofollow" target="_blank"><code>fs.createWriteStream()</code></a> is strongly recommended.</p>
<p>On Linux, positional writes do not work when the file is opened in append mode.
The kernel ignores the position argument and always appends the data to
the end of the file.</p>
<h4>filehandle.write(string[, position[, encoding]])<span><a class="mark" href="#fs_filehandle_write_string_position_encoding" id="fs_filehandle_write_string_position_encoding">#</a></span></h4><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li><code>string</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a></li>
<li><code>position</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> <strong>Default:</strong> <code>'utf8'</code></li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Write <code>string</code> to the file. If <code>string</code> is not a string, then
the value will be coerced to one.</p>
<p>The <code>Promise</code> is resolved with an object containing a <code>bytesWritten</code> property
identifying the number of bytes written, and a <code>buffer</code> property containing
a reference to the <code>string</code> written.</p>
<p><code>position</code> refers to the offset from the beginning of the file where this data
should be written. If the type of <code>position</code> is not a <code>number</code> the data
will be written at the current position. See <a href="http://nodejs.cn/s/2qJ9AM" rel="nofollow" target="_blank"><code>pwrite(2)</code></a>.</p>
<p><code>encoding</code> is the expected string encoding.</p>
<p>It is unsafe to use <code>filehandle.write()</code> multiple times on the same file
without waiting for the <code>Promise</code> to be resolved (or rejected). For this
scenario, <a href="http://nodejs.cn/s/VdSJQa" rel="nofollow" target="_blank"><code>fs.createWriteStream()</code></a> is strongly recommended.</p>
<p>On Linux, positional writes do not work when the file is opened in append mode.
The kernel ignores the position argument and always appends the data to
the end of the file.</p>
<h4>filehandle.writeFile(data, options)<span><a class="mark" href="#fs_filehandle_writefile_data_options" id="fs_filehandle_writefile_data_options">#</a></span></h4><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li><code>data</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/ZbDkpm" target="_blank" rel="nofollow" class="type">&lt;Uint8Array&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a> | <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a></p>
<ul>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/334hvC" target="_blank" rel="nofollow" class="type">&lt;null&gt;</a> <strong>Default:</strong> <code>'utf8'</code></li>
<li><code>mode</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <strong>Default:</strong> <code>0o666</code></li>
<li><code>flag</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> See <a href="http://nodejs.cn/s/JjbY8n" rel="nofollow" target="_blank">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'w'</code>.</li>
</ul>
</li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Asynchronously writes data to a file, replacing the file if it already exists.
<code>data</code> can be a string or a buffer. The <code>Promise</code> will be resolved with no
arguments upon success.</p>
<p>The <code>encoding</code> option is ignored if <code>data</code> is a buffer.</p>
<p>If <code>options</code> is a string, then it specifies the encoding.</p>
<p>The <code>FileHandle</code> has to support writing.</p>
<p>It is unsafe to use <code>filehandle.writeFile()</code> multiple times on the same file
without waiting for the <code>Promise</code> to be resolved (or rejected).</p>
<p>If one or more <code>filehandle.write()</code> calls are made on a file handle and then a
<code>filehandle.writeFile()</code> call is made, the data will be written from the
current position till the end of the file. It doesn't always write from the
beginning of the file.</p>
<h3>fsPromises.access(path[, mode])<span><a class="mark" href="#fs_fspromises_access_path_mode" id="fs_fspromises_access_path_mode">#</a></span></h3><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>mode</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <strong>Default:</strong> <code>fs.constants.F_OK</code></li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Tests a user's permissions for the file or directory specified by <code>path</code>.
The <code>mode</code> argument is an optional integer that specifies the accessibility
checks to be performed. Check <a href="http://nodejs.cn/s/qZfpqk" rel="nofollow" target="_blank">File Access Constants</a> for possible values
of <code>mode</code>. It is possible to create a mask consisting of the bitwise OR of
two or more values (e.g. <code>fs.constants.W_OK | fs.constants.R_OK</code>).</p>
<p>If the accessibility check is successful, the <code>Promise</code> is resolved with no
value. If any of the accessibility checks fail, the <code>Promise</code> is rejected
with an <code>Error</code> object. The following example checks if the file
<code>/etc/passwd</code> can be read and written by the current process.</p>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_keyword">const</span> fs <span class="sh_symbol">=</span> <span class="sh_function">require</span><span class="sh_symbol">(</span><span class="sh_string">'fs'</span><span class="sh_symbol">);</span>
<span class="sh_keyword">const</span> fsPromises <span class="sh_symbol">=</span> fs<span class="sh_symbol">.</span>promises<span class="sh_symbol">;</span>

fsPromises<span class="sh_symbol">.</span><span class="sh_function">access</span><span class="sh_symbol">(</span><span class="sh_string">'/etc/passwd'</span><span class="sh_symbol">,</span> fs<span class="sh_symbol">.</span>constants<span class="sh_symbol">.</span>R_OK <span class="sh_symbol">|</span> fs<span class="sh_symbol">.</span>constants<span class="sh_symbol">.</span>W_OK<span class="sh_symbol">)</span>
  <span class="sh_symbol">.</span><span class="sh_function">then</span><span class="sh_symbol">(()</span> <span class="sh_symbol">=&gt;</span> console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span><span class="sh_string">'can access'</span><span class="sh_symbol">))</span>
  <span class="sh_symbol">.</span><span class="sh_keyword">catch</span><span class="sh_symbol">(()</span> <span class="sh_symbol">=&gt;</span> console<span class="sh_symbol">.</span><span class="sh_function">error</span><span class="sh_symbol">(</span><span class="sh_string">'cannot access'</span><span class="sh_symbol">));</span>
</code></pre>
<p>Using <code>fsPromises.access()</code> to check for the accessibility of a file before
calling <code>fsPromises.open()</code> is not recommended. Doing so introduces a race
condition, since other processes may change the file's state between the two
calls. Instead, user code should open/read/write the file directly and handle
the error raised if the file is not accessible.</p>
<h3>fsPromises.appendFile(path, data[, options])<span><a class="mark" href="#fs_fspromises_appendfile_path_data_options" id="fs_fspromises_appendfile_path_data_options">#</a></span></h3><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a> | <a href="http://nodejs.cn/s/kLdifB" target="_blank" rel="nofollow" class="type">&lt;FileHandle&gt;</a> filename or <code>FileHandle</code></li>
<li><code>data</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a> | <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a></p>
<ul>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/334hvC" target="_blank" rel="nofollow" class="type">&lt;null&gt;</a> <strong>Default:</strong> <code>'utf8'</code></li>
<li><code>mode</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <strong>Default:</strong> <code>0o666</code></li>
<li><code>flag</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> See <a href="http://nodejs.cn/s/JjbY8n" rel="nofollow" target="_blank">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'a'</code>.</li>
</ul>
</li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Asynchronously append data to a file, creating the file if it does not yet
exist. <code>data</code> can be a string or a <a href="http://nodejs.cn/s/FApxjh" rel="nofollow" target="_blank"><code>Buffer</code></a>. The <code>Promise</code> will be
resolved with no arguments upon success.</p>
<p>If <code>options</code> is a string, then it specifies the encoding.</p>
<p>The <code>path</code> may be specified as a <code>FileHandle</code> that has been opened
for appending (using <code>fsPromises.open()</code>).</p>
<h3>fsPromises.chmod(path, mode)<span><a class="mark" href="#fs_fspromises_chmod_path_mode" id="fs_fspromises_chmod_path_mode">#</a></span></h3><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>mode</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Changes the permissions of a file then resolves the <code>Promise</code> with no
arguments upon succces.</p>
<h3>fsPromises.chown(path, uid, gid)<span><a class="mark" href="#fs_fspromises_chown_path_uid_gid" id="fs_fspromises_chown_path_uid_gid">#</a></span></h3><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>uid</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>gid</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Changes the ownership of a file then resolves the <code>Promise</code> with no arguments
upon success.</p>
<h3>fsPromises.copyFile(src, dest[, flags])<span><a class="mark" href="#fs_fspromises_copyfile_src_dest_flags" id="fs_fspromises_copyfile_src_dest_flags">#</a></span></h3><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li><code>src</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a> source filename to copy</li>
<li><code>dest</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a> destination filename of the copy operation</li>
<li><code>flags</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> modifiers for copy operation. <strong>Default:</strong> <code>0</code>.</li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Asynchronously copies <code>src</code> to <code>dest</code>. By default, <code>dest</code> is overwritten if it
already exists. The <code>Promise</code> will be resolved with no arguments upon success.</p>
<p>Node.js makes no guarantees about the atomicity of the copy operation. If an
error occurs after the destination file has been opened for writing, Node.js
will attempt to remove the destination.</p>
<p><code>flags</code> is an optional integer that specifies the behavior
of the copy operation. It is possible to create a mask consisting of the bitwise
OR of two or more values (e.g.
<code>fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE</code>).</p>
<ul>
<li><code>fs.constants.COPYFILE_EXCL</code> - The copy operation will fail if <code>dest</code> already
exists.</li>
<li><code>fs.constants.COPYFILE_FICLONE</code> - The copy operation will attempt to create a
copy-on-write reflink. If the platform does not support copy-on-write, then a
fallback copy mechanism is used.</li>
<li><code>fs.constants.COPYFILE_FICLONE_FORCE</code> - The copy operation will attempt to
create a copy-on-write reflink. If the platform does not support copy-on-write,
then the operation will fail.</li>
</ul>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_keyword">const</span> fsPromises <span class="sh_symbol">=</span> <span class="sh_function">require</span><span class="sh_symbol">(</span><span class="sh_string">'fs'</span><span class="sh_symbol">).</span>promises<span class="sh_symbol">;</span>

<span class="sh_comment">// destination.txt will be created or overwritten by default.</span>
fsPromises<span class="sh_symbol">.</span><span class="sh_function">copyFile</span><span class="sh_symbol">(</span><span class="sh_string">'source.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'destination.txt'</span><span class="sh_symbol">)</span>
  <span class="sh_symbol">.</span><span class="sh_function">then</span><span class="sh_symbol">(()</span> <span class="sh_symbol">=&gt;</span> console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span><span class="sh_string">'source.txt was copied to destination.txt'</span><span class="sh_symbol">))</span>
  <span class="sh_symbol">.</span><span class="sh_keyword">catch</span><span class="sh_symbol">(()</span> <span class="sh_symbol">=&gt;</span> console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span><span class="sh_string">'The file could not be copied'</span><span class="sh_symbol">));</span>
</code></pre>
<p>If the third argument is a number, then it specifies <code>flags</code>:</p>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_keyword">const</span> fs <span class="sh_symbol">=</span> <span class="sh_function">require</span><span class="sh_symbol">(</span><span class="sh_string">'fs'</span><span class="sh_symbol">);</span>
<span class="sh_keyword">const</span> fsPromises <span class="sh_symbol">=</span> fs<span class="sh_symbol">.</span>promises<span class="sh_symbol">;</span>
<span class="sh_keyword">const</span> <span class="sh_cbracket">{</span> COPYFILE_EXCL <span class="sh_cbracket">}</span> <span class="sh_symbol">=</span> fs<span class="sh_symbol">.</span>constants<span class="sh_symbol">;</span>

<span class="sh_comment">// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.</span>
fsPromises<span class="sh_symbol">.</span><span class="sh_function">copyFile</span><span class="sh_symbol">(</span><span class="sh_string">'source.txt'</span><span class="sh_symbol">,</span> <span class="sh_string">'destination.txt'</span><span class="sh_symbol">,</span> COPYFILE_EXCL<span class="sh_symbol">)</span>
  <span class="sh_symbol">.</span><span class="sh_function">then</span><span class="sh_symbol">(()</span> <span class="sh_symbol">=&gt;</span> console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span><span class="sh_string">'source.txt was copied to destination.txt'</span><span class="sh_symbol">))</span>
  <span class="sh_symbol">.</span><span class="sh_keyword">catch</span><span class="sh_symbol">(()</span> <span class="sh_symbol">=&gt;</span> console<span class="sh_symbol">.</span><span class="sh_function">log</span><span class="sh_symbol">(</span><span class="sh_string">'The file could not be copied'</span><span class="sh_symbol">));</span>
</code></pre>
<h3>fsPromises.lchmod(path, mode)<span><a class="mark" href="#fs_fspromises_lchmod_path_mode" id="fs_fspromises_lchmod_path_mode">#</a></span></h3><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>废弃于: v10.0.0</span>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>mode</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Changes the permissions on a symbolic link then resolves the <code>Promise</code> with
no arguments upon success. This method is only implemented on macOS.</p>
<h3>fsPromises.lchown(path, uid, gid)<span><a class="mark" href="#fs_fspromises_lchown_path_uid_gid" id="fs_fspromises_lchown_path_uid_gid">#</a></span></h3><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.6.0</td>
<td><p>This API is no longer deprecated.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p><span>新增于: v10.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>uid</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li><code>gid</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Changes the ownership on a symbolic link then resolves the <code>Promise</code> with
no arguments upon success.</p>
<h3>fsPromises.link(existingPath, newPath)<span><a class="mark" href="#fs_fspromises_link_existingpath_newpath" id="fs_fspromises_link_existingpath_newpath">#</a></span></h3><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li><code>existingPath</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>newPath</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Asynchronous <a href="http://nodejs.cn/s/U8H1mr" rel="nofollow" target="_blank"><code>link(2)</code></a>. The <code>Promise</code> is resolved with no arguments upon success.</p>
<h3>fsPromises.lstat(path[, options])<span><a class="mark" href="#fs_fspromises_lstat_path_options" id="fs_fspromises_lstat_path_options">#</a></span></h3><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.5.0</td>
<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p><span>新增于: v10.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a></p>
<ul>
<li><code>bigint</code> <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a> Whether the numeric values in the returned
<a href="http://nodejs.cn/s/NMuvVx" rel="nofollow" target="_blank"><code>fs.Stats</code></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li>
</ul>
</li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Asynchronous <a href="http://nodejs.cn/s/fGaHTu" rel="nofollow" target="_blank"><code>lstat(2)</code></a>. The <code>Promise</code> is resolved with the <a href="http://nodejs.cn/s/NMuvVx" rel="nofollow" target="_blank"><code>fs.Stats</code></a> object
for the given symbolic link <code>path</code>.</p>
<h3>fsPromises.mkdir(path[, options])<span><a class="mark" href="#fs_fspromises_mkdir_path_options" id="fs_fspromises_mkdir_path_options">#</a></span></h3><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a> | <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a></p>
<ul>
<li><code>recursive</code> <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a> <strong>Default:</strong> <code>false</code></li>
<li><code>mode</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> Not supported on Windows. <strong>Default:</strong> <code>0o777</code>.</li>
</ul>
</li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Asynchronously creates a directory then resolves the <code>Promise</code> with no
arguments upon success.</p>
<p>The optional <code>options</code> argument can be an integer specifying mode (permission
and sticky bits), or an object with a <code>mode</code> property and a <code>recursive</code>
property indicating whether parent folders should be created.</p>
<h3>fsPromises.mkdtemp(prefix[, options])<span><a class="mark" href="#fs_fspromises_mkdtemp_prefix_options" id="fs_fspromises_mkdtemp_prefix_options">#</a></span></h3><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li><code>prefix</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a></p>
<ul>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> <strong>Default:</strong> <code>'utf8'</code></li>
</ul>
</li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Creates a unique temporary directory and resolves the <code>Promise</code> with the created
folder path. A unique directory name is generated by appending six random
characters to the end of the provided <code>prefix</code>.</p>
<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
object with an <code>encoding</code> property specifying the character encoding to use.</p>
<pre class="sh_sourceCode"><code class="language-js">fsPromises<span class="sh_symbol">.</span><span class="sh_function">mkdtemp</span><span class="sh_symbol">(</span>path<span class="sh_symbol">.</span><span class="sh_function">join</span><span class="sh_symbol">(</span>os<span class="sh_symbol">.</span><span class="sh_function">tmpdir</span><span class="sh_symbol">(),</span> <span class="sh_string">'foo-'</span><span class="sh_symbol">))</span>
  <span class="sh_symbol">.</span><span class="sh_keyword">catch</span><span class="sh_symbol">(</span>console<span class="sh_symbol">.</span>error<span class="sh_symbol">);</span>
</code></pre>
<p>The <code>fsPromises.mkdtemp()</code> method will append the six randomly selected
characters directly to the <code>prefix</code> string. For instance, given a directory
<code>/tmp</code>, if the intention is to create a temporary directory <em>within</em> <code>/tmp</code>, the
<code>prefix</code> must end with a trailing platform-specific path separator
(<code>require('path').sep</code>).</p>
<h3>fsPromises.open(path, flags[, mode])<span><a class="mark" href="#fs_fspromises_open_path_flags_mode" id="fs_fspromises_open_path_flags_mode">#</a></span></h3><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v11.1.0</td>
<td><p>The <code>flags</code> argument is now optional and defaults to <code>'r'</code>.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p><span>新增于: v10.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>flags</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> See <a href="http://nodejs.cn/s/JjbY8n" rel="nofollow" target="_blank">support of file system <code>flags</code></a>.
<strong>Default:</strong> <code>'r'</code>.</li>
<li><code>mode</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <strong>Default:</strong> <code>0o666</code> (readable and writable)</li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Asynchronous file open that returns a <code>Promise</code> that, when resolved, yields a
<code>FileHandle</code> object. See <a href="http://nodejs.cn/s/ss2dGs" rel="nofollow" target="_blank"><code>open(2)</code></a>.</p>
<p><code>mode</code> sets the file mode (permission and sticky bits), but only if the file was
created.</p>
<p>Some characters (<code>&lt; &gt; : " / \ | ? *</code>) are reserved under Windows as documented
by <a href="http://nodejs.cn/s/GmL95W" rel="nofollow" target="_blank">Naming Files, Paths, and Namespaces</a>. Under NTFS, if the filename contains
a colon, Node.js will open a file system stream, as described by
<a href="http://nodejs.cn/s/FY8iKV" rel="nofollow" target="_blank">this MSDN page</a>.</p>
<h3>fsPromises.readdir(path[, options])<span><a class="mark" href="#fs_fspromises_readdir_path_options" id="fs_fspromises_readdir_path_options">#</a></span></h3><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.11.0</td>
<td><p>New option <code>withFileTypes</code> was added.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p><span>新增于: v10.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a></p>
<ul>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> <strong>Default:</strong> <code>'utf8'</code></li>
<li><code>withFileTypes</code> <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a> <strong>Default:</strong> <code>false</code></li>
</ul>
</li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Reads the contents of a directory then resolves the <code>Promise</code> with an array
of the names of the files in the directory excluding <code>'.'</code> and <code>'..'</code>.</p>
<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
object with an <code>encoding</code> property specifying the character encoding to use for
the filenames. If the <code>encoding</code> is set to <code>'buffer'</code>, the filenames returned
will be passed as <code>Buffer</code> objects.</p>
<p>If <code>options.withFileTypes</code> is set to <code>true</code>, the resolved array will contain
<a href="http://nodejs.cn/s/mNLVJ7" rel="nofollow" target="_blank"><code>fs.Dirent</code></a> objects.</p>
<h3>fsPromises.readFile(path[, options])<span><a class="mark" href="#fs_fspromises_readfile_path_options" id="fs_fspromises_readfile_path_options">#</a></span></h3><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a> | <a href="http://nodejs.cn/s/kLdifB" target="_blank" rel="nofollow" class="type">&lt;FileHandle&gt;</a> filename or <code>FileHandle</code></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a> | <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a></p>
<ul>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/334hvC" target="_blank" rel="nofollow" class="type">&lt;null&gt;</a> <strong>Default:</strong> <code>null</code></li>
<li><code>flag</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> See <a href="http://nodejs.cn/s/JjbY8n" rel="nofollow" target="_blank">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'r'</code>.</li>
</ul>
</li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Asynchronously reads the entire contents of a file.</p>
<p>The <code>Promise</code> is resolved with the contents of the file. If no encoding is
specified (using <code>options.encoding</code>), the data is returned as a <code>Buffer</code>
object. Otherwise, the data will be a string.</p>
<p>If <code>options</code> is a string, then it specifies the encoding.</p>
<p>When the <code>path</code> is a directory, the behavior of <code>fsPromises.readFile()</code> is
platform-specific. On macOS, Linux, and Windows, the promise will be rejected
with an error. On FreeBSD, a representation of the directory's contents will be
returned.</p>
<p>Any specified <code>FileHandle</code> has to support reading.</p>
<h3>fsPromises.readlink(path[, options])<span><a class="mark" href="#fs_fspromises_readlink_path_options" id="fs_fspromises_readlink_path_options">#</a></span></h3><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a></p>
<ul>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> <strong>Default:</strong> <code>'utf8'</code></li>
</ul>
</li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Asynchronous <a href="http://nodejs.cn/s/CFw99K" rel="nofollow" target="_blank"><code>readlink(2)</code></a>. The <code>Promise</code> is resolved with the <code>linkString</code> upon
success.</p>
<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
object with an <code>encoding</code> property specifying the character encoding to use for
the link path returned. If the <code>encoding</code> is set to <code>'buffer'</code>, the link path
returned will be passed as a <code>Buffer</code> object.</p>
<h3>fsPromises.realpath(path[, options])<span><a class="mark" href="#fs_fspromises_realpath_path_options" id="fs_fspromises_realpath_path_options">#</a></span></h3><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a></p>
<ul>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> <strong>Default:</strong> <code>'utf8'</code></li>
</ul>
</li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Determines the actual location of <code>path</code> using the same semantics as the
<code>fs.realpath.native()</code> function then resolves the <code>Promise</code> with the resolved
path.</p>
<p>Only paths that can be converted to UTF8 strings are supported.</p>
<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
object with an <code>encoding</code> property specifying the character encoding to use for
the path. If the <code>encoding</code> is set to <code>'buffer'</code>, the path returned will be
passed as a <code>Buffer</code> object.</p>
<p>On Linux, when Node.js is linked against musl libc, the procfs file system must
be mounted on <code>/proc</code> in order for this function to work. Glibc does not have
this restriction.</p>
<h3>fsPromises.rename(oldPath, newPath)<span><a class="mark" href="#fs_fspromises_rename_oldpath_newpath" id="fs_fspromises_rename_oldpath_newpath">#</a></span></h3><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li><code>oldPath</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>newPath</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Renames <code>oldPath</code> to <code>newPath</code> and resolves the <code>Promise</code> with no arguments
upon success.</p>
<h3>fsPromises.rmdir(path)<span><a class="mark" href="#fs_fspromises_rmdir_path" id="fs_fspromises_rmdir_path">#</a></span></h3><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Removes the directory identified by <code>path</code> then resolves the <code>Promise</code> with
no arguments upon success.</p>
<p>Using <code>fsPromises.rmdir()</code> on a file (not a directory) results in the
<code>Promise</code> being rejected with an <code>ENOENT</code> error on Windows and an <code>ENOTDIR</code>
error on POSIX.</p>
<h3>fsPromises.stat(path[, options])<span><a class="mark" href="#fs_fspromises_stat_path_options" id="fs_fspromises_stat_path_options">#</a></span></h3><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<details class="changelog"><summary>版本历史</summary>
<table>
<tbody><tr><th>版本</th><th>变更</th></tr>
<tr><td>v10.5.0</td>
<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p><span>新增于: v10.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a></p>
<ul>
<li><code>bigint</code> <a href="http://nodejs.cn/s/jFbvuT" target="_blank" rel="nofollow" class="type">&lt;boolean&gt;</a> Whether the numeric values in the returned
<a href="http://nodejs.cn/s/NMuvVx" rel="nofollow" target="_blank"><code>fs.Stats</code></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li>
</ul>
</li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>The <code>Promise</code> is resolved with the <a href="http://nodejs.cn/s/NMuvVx" rel="nofollow" target="_blank"><code>fs.Stats</code></a> object for the given <code>path</code>.</p>
<h3>fsPromises.symlink(target, path[, type])<span><a class="mark" href="#fs_fspromises_symlink_target_path_type" id="fs_fspromises_symlink_target_path_type">#</a></span></h3><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li><code>target</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>type</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> <strong>Default:</strong> <code>'file'</code></li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Creates a symbolic link then resolves the <code>Promise</code> with no arguments upon
success.</p>
<p>The <code>type</code> argument is only used on Windows platforms and can be one of <code>'dir'</code>,
<code>'file'</code>, or <code>'junction'</code>. Windows junction points require the destination path
to be absolute. When using <code>'junction'</code>, the <code>target</code> argument will
automatically be normalized to absolute path.</p>
<h3>fsPromises.truncate(path[, len])<span><a class="mark" href="#fs_fspromises_truncate_path_len" id="fs_fspromises_truncate_path_len">#</a></span></h3><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>len</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <strong>Default:</strong> <code>0</code></li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Truncates the <code>path</code> then resolves the <code>Promise</code> with no arguments upon
success. The <code>path</code> <em>must</em> be a string or <code>Buffer</code>.</p>
<h3>fsPromises.unlink(path)<span><a class="mark" href="#fs_fspromises_unlink_path" id="fs_fspromises_unlink_path">#</a></span></h3><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Asynchronous <a href="http://nodejs.cn/s/gjRRsM" rel="nofollow" target="_blank"><code>unlink(2)</code></a>. The <code>Promise</code> is resolved with no arguments upon
success.</p>
<h3>fsPromises.utimes(path, atime, mtime)<span><a class="mark" href="#fs_fspromises_utimes_path_atime_mtime" id="fs_fspromises_utimes_path_atime_mtime">#</a></span></h3><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li><code>path</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a></li>
<li><code>atime</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> | <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/A9TMoa" target="_blank" rel="nofollow" class="type">&lt;Date&gt;</a></li>
<li><code>mtime</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;number&gt;</a> | <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/A9TMoa" target="_blank" rel="nofollow" class="type">&lt;Date&gt;</a></li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Change the file system timestamps of the object referenced by <code>path</code> then
resolves the <code>Promise</code> with no arguments upon success.</p>
<p>The <code>atime</code> and <code>mtime</code> arguments follow these rules:</p>
<ul>
<li>Values can be either numbers representing Unix epoch time, <code>Date</code>s, or a
numeric string like <code>'123456789.0'</code>.</li>
<li>If the value can not be converted to a number, or is <code>NaN</code>, <code>Infinity</code> or
<code>-Infinity</code>, an <code>Error</code> will be thrown.</li>
</ul>
<h3>fsPromises.writeFile(file, data[, options])<span><a class="mark" href="#fs_fspromises_writefile_file_data_options" id="fs_fspromises_writefile_file_data_options">#</a></span></h3><p class="tip_wrap"><span class="tip_no_trans">暂无中英对照</span></p>
<div class="api_metadata">
<span>新增于: v10.0.0</span>
</div>
<ul>
<li><code>file</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/5dwq7G" target="_blank" rel="nofollow" class="type">&lt;URL&gt;</a> | <a href="http://nodejs.cn/s/kLdifB" target="_blank" rel="nofollow" class="type">&lt;FileHandle&gt;</a> filename or <code>FileHandle</code></li>
<li><code>data</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/6x1hD3" target="_blank" rel="nofollow" class="type">&lt;Buffer&gt;</a> | <a href="http://nodejs.cn/s/ZbDkpm" target="_blank" rel="nofollow" class="type">&lt;Uint8Array&gt;</a></li>
<li>
<p><code>options</code> <a href="http://nodejs.cn/s/jzn6Ao" target="_blank" rel="nofollow" class="type">&lt;Object&gt;</a> | <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a></p>
<ul>
<li><code>encoding</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> | <a href="http://nodejs.cn/s/334hvC" target="_blank" rel="nofollow" class="type">&lt;null&gt;</a> <strong>Default:</strong> <code>'utf8'</code></li>
<li><code>mode</code> <a href="http://nodejs.cn/s/SXbo1v" target="_blank" rel="nofollow" class="type">&lt;integer&gt;</a> <strong>Default:</strong> <code>0o666</code></li>
<li><code>flag</code> <a href="http://nodejs.cn/s/9Tw2bK" target="_blank" rel="nofollow" class="type">&lt;string&gt;</a> See <a href="http://nodejs.cn/s/JjbY8n" rel="nofollow" target="_blank">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'w'</code>.</li>
</ul>
</li>
<li>Returns: <a href="http://nodejs.cn/s/ri1kj8" target="_blank" rel="nofollow" class="type">&lt;Promise&gt;</a></li>
</ul>
<p>Asynchronously writes data to a file, replacing the file if it already exists.
<code>data</code> can be a string or a buffer. The <code>Promise</code> will be resolved with no
arguments upon success.</p>
<p>The <code>encoding</code> option is ignored if <code>data</code> is a buffer.</p>
<p>If <code>options</code> is a string, then it specifies the encoding.</p>
<p>Any specified <code>FileHandle</code> has to support writing.</p>
<p>It is unsafe to use <code>fsPromises.writeFile()</code> multiple times on the same file
without waiting for the <code>Promise</code> to be resolved (or rejected).</p>
<h2>FS 常量<span><a class="mark" href="#fs_fs_constants_1" id="fs_fs_constants_1">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/fs_constants_1.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/ebwL4o" class="tip_en">提交修改</a></p>
<p>以下常量由 <code>fs.constants</code> 输出。</p>
<p>并非所有操作系统都可以使用每个常量。</p>
<h3>文件可访问性的常量<span><a class="mark" href="#fs_file_access_constants" id="fs_file_access_constants">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/file_access_constants.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/fcskRv" class="tip_en">提交修改</a></p>
<p>以下常量适用于 <a href="http://nodejs.cn/s/NCPsM3" rel="nofollow" target="_blank"><code>fs.access()</code></a>。</p>
<table>
  <tbody><tr>
    <th>常量</th>
    <th>说明</th>
  </tr>
  <tr>
    <td><code>F_OK</code></td>
    <td>表明文件对调用进程可见。
    这对于判断文件是否存在很有用，但对 <code>rwx</code> 权限没有任何说明。 
    如果未指定模式，则默认值为该值。
    </td>
  </tr>
  <tr>
    <td><code>R_OK</code></td>
    <td>表明调用进程可以读取文件。</td>
  </tr>
  <tr>
    <td><code>W_OK</code></td>
    <td>表明调用进程可以写入文件。</td>
  </tr>
  <tr>
    <td><code>X_OK</code></td>
    <td>表明调用进程可以执行文件。
    在 Windows 上无效（表现得像 <code>fs.constants.F_OK</code>）。
    </td>
  </tr>
</tbody></table>
<h3>文件拷贝的常量<span><a class="mark" href="#fs_file_copy_constants" id="fs_file_copy_constants">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/file_copy_constants.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/ZrzTM5" class="tip_en">提交修改</a></p>
<p>以下常量适用于 <a href="http://nodejs.cn/s/T2U5cj" rel="nofollow" target="_blank"><code>fs.copyFile()</code></a>。</p>
<table>
  <tbody><tr>
    <th>常量</th>
    <th>说明</th>
  </tr>
  <tr>
    <td><code>COPYFILE_EXCL</code></td>
    <td>如果目标路径已存在，则拷贝操作将失败。</td>
  </tr>
  <tr>
    <td><code>COPYFILE_FICLONE</code></td>
    <td>拷贝操作将尝试创建写时拷贝链接。
    如果底层平台不支持写时拷贝，则使用备选的拷贝机制。
    </td>
  </tr>
  <tr>
    <td><code>COPYFILE_FICLONE_FORCE</code></td>
    <td>拷贝操作将尝试创建写时拷贝链接。
    如果底层平台不支持写时拷贝，则拷贝操作将失败。
    </td>
  </tr>
</tbody></table>
<h3>文件打开的常量<span><a class="mark" href="#fs_file_open_constants" id="fs_file_open_constants">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/file_open_constants.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/CK8m8Z" class="tip_en">提交修改</a></p>
<p>以下常量适用于 <code>fs.open()</code>。</p>
<table>
  <tbody><tr>
    <th>常量</th>
    <th>说明</th>
  </tr>
  <tr>
    <td><code>O_RDONLY</code></td>
    <td>表明打开文件用于只读访问。</td>
  </tr>
  <tr>
    <td><code>O_WRONLY</code></td>
    <td>表明打开文件用于只写访问。</td>
  </tr>
  <tr>
    <td><code>O_RDWR</code></td>
    <td>表明打开文件用于读写访问。</td>
  </tr>
  <tr>
    <td><code>O_CREAT</code></td>
    <td>表明如果文件尚不存在则创建该文件。</td>
  </tr>
  <tr>
    <td><code>O_EXCL</code></td>
    <td>表明如果设置了 <code>O_CREAT</code> 标志且文件已存在，则打开文件应该失败。</td>
  </tr>
  <tr>
    <td><code>O_NOCTTY</code></td>
    <td>表明如果路径是终端设备，则打开该路径不应该造成该终端变成进程的控制终端（如果进程还没有终端）。</td>
  </tr>
  <tr>
    <td><code>O_TRUNC</code></td>
    <td>表明如果文件存在且是常规文件、并且文件成功打开以进行写入访问，则其长度应截断为零。</td>
  </tr>
  <tr>
    <td><code>O_APPEND</code></td>
    <td>表明数据将追加到文件末尾。</td>
  </tr>
  <tr>
    <td><code>O_DIRECTORY</code></td>
    <td>表明如果路径不是目录，则打开应该失败。</td>
  </tr>
  <tr>
  <td><code>O_NOATIME</code></td>
    <td>表明文件系统的读取访问将不再导致与文件相关联的 <code>atime</code> 信息的更新。
    仅在 Linux 操作系统上可用。</td>
  </tr>
  <tr>
    <td><code>O_NOFOLLOW</code></td>
    <td>表明如果路径是符号链接，则打开应该失败。</td>
  </tr>
  <tr>
    <td><code>O_SYNC</code></td>
    <td>表明文件是为同步 I/O 打开的，写入操作将等待文件的完整性。</td>
  </tr>
  <tr>
    <td><code>O_DSYNC</code></td>
    <td>表明文件是为同步 I/O 打开的，写入操作将等待数据的完整性</td>
  </tr>
  <tr>
    <td><code>O_SYMLINK</code></td>
    <td>表明打开符号链接自身，而不是它指向的资源。</td>
  </tr>
  <tr>
    <td><code>O_DIRECT</code></td>
    <td>表明将尝试最小化文件 I/O 的缓存效果。</td>
  </tr>
  <tr>
    <td><code>O_NONBLOCK</code></td>
    <td>表明在可能的情况下以非阻塞模式打开文件。</td>
  </tr>
</tbody></table>
<h3>文件类型的常量<span><a class="mark" href="#fs_file_type_constants" id="fs_file_type_constants">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/file_type_constants.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/uRbvNy" class="tip_en">提交修改</a></p>
<p>以下常量适用于 <a href="http://nodejs.cn/s/NMuvVx" rel="nofollow" target="_blank"><code>fs.Stats</code></a> 对象的 <code>mode</code> 属性，用于决定文件的类型。</p>
<table>
  <tbody><tr>
    <th>常量</th>
    <th>说明</th>
  </tr>
  <tr>
    <td><code>S_IFMT</code></td>
    <td>用于提取文件类型代码的位掩码。</td>
  </tr>
  <tr>
    <td><code>S_IFREG</code></td>
    <td>表示常规文件。</td>
  </tr>
  <tr>
    <td><code>S_IFDIR</code></td>
    <td>表示目录。</td>
  </tr>
  <tr>
    <td><code>S_IFCHR</code></td>
    <td>表示面向字符的设备文件。</td>
  </tr>
  <tr>
    <td><code>S_IFBLK</code></td>
    <td>表示面向块的设备文件。</td>
  </tr>
  <tr>
    <td><code>S_IFIFO</code></td>
    <td>表示 FIFO 或管道。</td>
  </tr>
  <tr>
    <td><code>S_IFLNK</code></td>
    <td>表示符号链接。</td>
  </tr>
  <tr>
    <td><code>S_IFSOCK</code></td>
    <td>表示套接字。</td>
  </tr>
</tbody></table>
<h3>文件模式的常量<span><a class="mark" href="#fs_file_mode_constants" id="fs_file_mode_constants">#</a></span></h3><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/file_mode_constants.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/RMTMoM" class="tip_en">提交修改</a></p>
<p>以下常量适用于 <a href="http://nodejs.cn/s/NMuvVx" rel="nofollow" target="_blank"><code>fs.Stats</code></a> 对象的 <code>mode</code> 属性，用于决定文件的访问权限。</p>
<table>
  <tbody><tr>
    <th>常量</th>
    <th>说明</th>
  </tr>
  <tr>
    <td><code>S_IRWXU</code></td>
    <td>表明所有者可读、可写、可执行。</td>
  </tr>
  <tr>
    <td><code>S_IRUSR</code></td>
    <td>表明所有者可读。</td>
  </tr>
  <tr>
    <td><code>S_IWUSR</code></td>
    <td>表明所有者可写。</td>
  </tr>
  <tr>
    <td><code>S_IXUSR</code></td>
    <td>表明所有者可执行。</td>
  </tr>
  <tr>
    <td><code>S_IRWXG</code></td>
    <td>表明群组可读、可写、可执行。</td>
  </tr>
  <tr>
    <td><code>S_IRGRP</code></td>
    <td>表明群组可读。</td>
  </tr>
  <tr>
    <td><code>S_IWGRP</code></td>
    <td>表明群组可写。</td>
  </tr>
  <tr>
    <td><code>S_IXGRP</code></td>
    <td>表明群组可执行。</td>
  </tr>
  <tr>
    <td><code>S_IRWXO</code></td>
    <td>表明其他人可读、可写、可执行。</td>
  </tr>
  <tr>
    <td><code>S_IROTH</code></td>
    <td>表明其他人可读。</td>
  </tr>
  <tr>
    <td><code>S_IWOTH</code></td>
    <td>表明其他人可写。</td>
  </tr>
  <tr>
    <td><code>S_IXOTH</code></td>
    <td>表明其他人可执行。</td>
  </tr>
</tbody></table>
<h2>文件系统标志<span><a class="mark" href="#fs_file_system_flags" id="fs_file_system_flags">#</a></span></h2><p class="tip_wrap"><a href="http://nodejs.cn/api/fs/file_system_flags.html" class="tip_trans">中英对照</a><a rel="nofollow" target="_blank" href="http://nodejs.cn/s/6tUgtd" class="tip_en">提交修改</a></p>
<p>当 <code>flag</code> 选项采用字符串时，可用以下标志：</p>
<ul>
<li>
<p><code>'a'</code> - 打开文件用于追加。如果文件不存在，则创建该文件。</p>
</li>
<li>
<p><code>'ax'</code> - 与 <code>'a'</code> 相似，但如果路径已存在则失败。</p>
</li>
<li>
<p><code>'a+'</code> - 打开文件用于读取和追加。如果文件不存在，则创建该文件。</p>
</li>
<li>
<p><code>'ax+'</code> - 与 <code>'a+'</code> 相似，但如果路径已存在则失败。</p>
</li>
<li>
<p><code>'as'</code> - 以同步模式打开文件用于追加。如果文件不存在，则创建该文件。</p>
</li>
<li>
<p><code>'as+'</code> - 以同步模式打开文件用于读取和追加。如果文件不存在，则创建该文件。</p>
</li>
<li>
<p><code>'r'</code> - 打开文件用于读取。如果文件不存在，则出现异常。</p>
</li>
<li>
<p><code>'r+'</code> - 打开文件用于读取和写入。如果文件不存在，则出现异常。</p>
</li>
<li>
<p><code>'rs+'</code> - 以同步模式打开文件用于读取和写入。指示操作系统绕过本地的文件系统缓存。</p>
<p>这对于在 NFS 挂载上打开文件时非常有用，因为它允许跳过可能过时的本地缓存。
它对 I/O 性能有非常实际的影响，因此除非需要，否则不建议使用此标志。</p>
<p>这不会将 <code>fs.open()</code> 或 <code>fsPromises.open()</code> 转换为同步的阻塞调用。
如果需要同步的操作，则应使用 <code>fs.openSync()</code> 之类的。</p>
</li>
<li>
<p><code>'w'</code> - 打开文件用于写入。如果文件不存在则创建文件，如果文件已存在则截断文件。</p>
</li>
<li>
<p><code>'wx'</code> - 与 <code>'w'</code> 相似，但如果路径已存在则失败。</p>
</li>
<li>
<p><code>'w+'</code> - 打开文件用于读取和写入。如果文件不存在则创建文件，如果文件已存在则截断文件。</p>
</li>
<li>
<p><code>'wx+'</code> - 与 <code>'w+'</code> 相似，但如果路径已存在则失败。</p>
</li>
</ul>
<p><code>flag</code> 也可以是一个数字，参阅 <a href="http://nodejs.cn/s/ss2dGs" rel="nofollow" target="_blank"><code>open(2)</code></a> 文档。
常用的常量可以从 <code>fs.constants</code> 获取。
在 Windows 上，标志会被适当地转换为等效的标志，例如 <code>O_WRONLY</code> 转换为 <code>FILE_GENERIC_WRITE</code>、 <code>O_EXCL|O_CREAT</code> 转换为能被 <code>CreateFileW</code> 接受的 <code>CREATE_NEW</code>。</p>
<p>特有的 <code>'x'</code> 标志（ <a href="http://nodejs.cn/s/ss2dGs" rel="nofollow" target="_blank"><code>open(2)</code></a> 中的 <code>O_EXCL</code> 标志）可以确保路径是新创建的。
在 POSIX 系统上，即使路径是一个符号链接且指向一个不存在的文件，它也会被视为已存在。
该特有标志不一定适用于网络文件系统。</p>
<p>在 Linux 上，当以追加模式打开文件时，写入无法指定位置。
内核会忽略位置参数，并始终将数据追加到文件的末尾。</p>
<p>如果要修改文件而不是覆盖文件，则标志模式应为 <code>'r+'</code> 模式而不是默认的 <code>'w'</code> 模式。</p>
<p>某些标志的行为是特定于平台的。
例如，在 macOS 和 Linux 上使用 <code>'a+'</code> 标志打开目录（参阅下面的示例）会返回一个错误。
而在 Windows 和 FreeBSD 上，则返回一个文件描述符或 <code>FileHandle</code>。</p>
<pre class="sh_sourceCode"><code class="language-js"><span class="sh_comment">// 在 macOS 和 Linux 上：</span>
fs<span class="sh_symbol">.</span><span class="sh_function">open</span><span class="sh_symbol">(</span><span class="sh_string">'&lt;目录&gt;'</span><span class="sh_symbol">,</span> <span class="sh_string">'a+'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">,</span> fd<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_comment">// =&gt; [Error: EISDIR: illegal operation on a directory, open &lt;目录&gt;]</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>

<span class="sh_comment">// 在 Windows 和 FreeBSD 上：</span>
fs<span class="sh_symbol">.</span><span class="sh_function">open</span><span class="sh_symbol">(</span><span class="sh_string">'&lt;目录&gt;'</span><span class="sh_symbol">,</span> <span class="sh_string">'a+'</span><span class="sh_symbol">,</span> <span class="sh_symbol">(</span>err<span class="sh_symbol">,</span> fd<span class="sh_symbol">)</span> <span class="sh_symbol">=&gt;</span> <span class="sh_cbracket">{</span>
  <span class="sh_comment">// =&gt; null, &lt;fd&gt;</span>
<span class="sh_cbracket">}</span><span class="sh_symbol">);</span>
</code></pre>
<p>在 Windows 上，使用 <code>'w'</code> 标志打开现存的隐藏文件（通过 <code>fs.open()</code>、 <code>fs.writeFile()</code> 或 <code>fsPromises.open()</code>）会抛出 <code>EPERM</code>。
现存的隐藏文件可以使用 <code>'r+'</code> 标志打开用于写入。</p>
<p>调用 <code>fs.ftruncate()</code> 或 <code>fsPromises.ftruncate()</code> 可以用于重置文件的内容。</p>
        </div>
      </div>
    </div>
  </div>
  <!-- 搜索弹窗 -->
  <!-- <div id="search_modal"></div> -->
  <!-- js -->
  <!--[if lt IE 10]><script src="/static/lib/placeholders.js?20170426"></script><![endif]-->
  
  <script>
    document.write('<script src="' + (location.port ? '' : '//static.nodejs.cn') + '/_static/js/api.js?1558626669267"><\/script>');
  </script><script src="./Nodejs fs Driver_files/api.js"></script>

<div id="hm_t_undefined" class="hm-t-container"><div class="hm-t-go-top" style="display: none;"></div></div></body></html>